VBScript sendKeys已打开IE窗口

问题描述 投票:0回答:1

我是VBScript的新手,并且关于具有已打开的IE窗口的sendKey的问题。

最初,我有一个脚本,该脚本打开了Internet Explorer的新实例,在文本字段中写了一些文本,然后单击按钮:

Set shell = WScript.CreateObject("WScript.shell")
Set IE = WScript.CreateObject("InternetExplorer.Application", "ie_")

Wait IE, 2000
IE.Visible = True
shell.AppActivate IE

Wait IE, 2000
IE.Navigate "http://someurl"

Wait IE, 2000
IE.document.getElementById("myTextField").Click
shell.SendKeys "TEST"
Wait IE, 2000
IE.document.getElementById("myButton").Click
...
IE.Quit

End Function

Sub Wait(IE, ms)
    Do
        WScript.Sleep ms
    Loop While IE.ReadyState < 4 And IE.Busy
End Sub

这很好用。但是,现在我需要对已经打开的IE实例执行相同的操作,这就是我所做的:

Set shell = WScript.CreateObject("WScript.shell")

Dim objInstances, objIE, IE
Set objInstances = WScript.CreateObject("Shell.Application").windows
If objInstances.Count > 0 Then '/// make sure we have instances open.
    For Each objIE In objInstances
        If InStr(objIE.LocationURL,"http://someurl") > 0 then
        Set IE = objIE
        End if
Next
End If
shell.AppActivate ("Internet Explorer")

Wait IE, 2000
IE.document.getElementById("myTextField").Click
shell.SendKeys "TEST"
Wait IE, 2000
IE.document.getElementById("myButton").Click
...
IE.Quit

End Function

Sub Wait(IE, ms)
    Do
        WScript.Sleep ms
    Loop While IE.ReadyState < 4 And IE.Busy
End Sub

这次,文本不会被写入文本字段。有没有人对此有解决方案?

internet-explorer vbscript sendkeys
1个回答
0
投票

如果要向文本字段发送值,则可以使用IE.document.getElementById("myTextField").Value

更改这两行:

IE.document.getElementById("myTextField").Click
shell.SendKeys "TEST"

进入:

IE.document.getElementById("myTextField").Value = "TEST"

我也制作了一个示例代码来自动化google.com,它可以在IE中很好地工作。您可以参考它:

Set shell = WScript.CreateObject("WScript.shell")

Dim objInstances, objIE, IE
Set objInstances = WScript.CreateObject("Shell.Application").windows
If objInstances.Count > 0 Then 
    For Each objIE In objInstances
        If InStr(objIE.LocationURL,"https://www.google.com") > 0 then
        Set IE = objIE
        End if
Next
End If
shell.AppActivate ("Internet Explorer")

Wait IE, 2000
IE.document.getElementsByName("q")(0).Value = "TEST"
Wait IE, 2000
IE.document.getElementsByName("btnK")(1).Click
'IE.Quit    

Sub Wait(IE, ms)
    Do
        WScript.Sleep ms
    Loop While IE.ReadyState < 4 And IE.Busy
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.