LoginSignup
15
20

More than 5 years have passed since last update.

IEへのキー入力をVBScriptで再現する。

Last updated at Posted at 2014-08-22

はじめに

InternetExplorerへのキー入力(ex.Enter,Tab)をVBScriptで再現する方法が、実に面倒だったのでシェアします。
起動したIEのプロセスIDを取得し、それを使ってIEをアクティブにする。その上でSendkeysを使ってキー入力を再現する。

コード

IEキー入力.vbs
'IEキー入力.vbs
'Copyright (c) 2014 nezuq
'This software is released under the MIT License.
'http://opensource.org/licenses/mit-license.php

'シェルを起動する
Dim wsh
Set wsh = WScript.CreateObject("WScript.Shell")

'IEを起動する
Dim ie 
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'ie.FullScreen = True

'IEをアクティブにする
Dim wLoc, wSvc, wEnu, wIns
Set wLoc = CreateObject("WbemScripting.SWbemLocator")
Set wSvc = wLoc.ConnectServer
Set wEnu = wSvc.InstancesOf("Win32_Process")
Dim pId
For Each wIns in wEnu
    If Not IsEmpty(wIns.ProcessId) And wIns.Description = "iexplore.exe" Then
        pId = wIns.ProcessId
    End If
Next
Set wLoc = Nothing
Set wEnu = Nothing
Set wSvc = Nothing
While not wsh.AppActivate(pId) 
    Wscript.Sleep 100 
Wend 

'IEで指定URLを開く
ie.Navigate "https://secure.nicovideo.jp/secure/login_form"
Do While ie.Busy = True Or ie.readyState <> 4
Loop

'IEで要素に値を入れる
Dim elm 
Set elm = ie.document.getElementById("mail")
elm.Value = "メールアドレス"

'IEでキー入力を再現する
Wscript.Sleep 1000 
wsh.SendKeys "{tab}"
Wscript.Sleep 1000 
wsh.SendKeys "password" '日本語不可

'5秒停止しIEを閉じる
WScript.Sleep 5 * 1000
ie.Quit

Set elm = Nothing
Set ie = Nothing
Set wsh = Nothing

ニコニコ動画のログイン画面が自動で開き、入力欄に値が入ります。その後、ログインせずにIEが閉じます。

参考記事

VBS(WSH)で開いたIEのウィンドウがアクティブにならない 【OKWave】

15
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
20