我试图弄清楚如何在已经打开的网页上操作表单字段(文本框和按钮),而无需打开新的Internet Explorer实例,也无需先导航到特定的URL。 / p>
我目前有以下代码可以打开Google,填写搜索字段,然后点击Google搜索按钮:
Sub Main
Dim objIE
Dim UserN
Dim url As String
url = "http://www.google.com"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.navigate url
WaitForLoad (objIE)
' runs the sub "Wait for Load" that waits until the page is fully loaded before continuing the script
MsgBox "It Loaded"
Set UserN = objIE.document.getElementsByName("q")
If Not UserN Is Nothing Then
' fill in first element named "q", assumed to be the login name field
UserN(0).value = "Dragon NaturallySpeaking"
End If
Wait 2
Dim f
Set f = objIE.document.getElementsByName("btnK")
f(0).click
End Sub
Sub WaitForLoad (objIE)
Do While objIE.Busy
Wait .1
Loop
End Sub
我目前的问题是,我希望能够填写搜索字段并点击Google搜索按钮,而无需先导航到www.google.com。例如,我已经打开浏览器并访问www.google.com,但现在我想执行填写文本字段并单击按钮的脚本。不幸的是,我找不到办法做到这一点。我尝试使用Set objIE = GetObject(“InternetExplorer.Application”) - 而不是CreateObject - 并删除objIE.Visible = True和ObjIE.Navigate url,但这只会返回错误。