好吧,我将我的excel VBA宏插入了我的网站,并从着陆点将其定向到搜索页面,但是在IE到达搜索页面后,我得到了This Error。我以为在新页面上重置objcollection可以,但是我不知道如何设置焦点或选择新页面,或者什么……我对使用VBA和IE进行自动化操作感到陌生,
Sub GoToWebsiteTest2()
Dim appIE As InternetExplorerMedium
Dim objElement As Object
Dim objCollection As Object
Set appIE = New InternetExplorerMedium
sURL = "https://example.login.aspx"
With appIE
.Navigate sURL
.Visible = True
End With
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Set objCollection = appIE.Document.getElementsByTagName
'Set appIE = Nothing
appIE.Document.getElementById("txtUsername").Value = "user"
appIE.Document.getElementById("txtPassword").Value = "Pass"
appIE.Document.getElementById("btnLogin").Click
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
appIE.Navigate "https://example.Search.aspx"
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Set objCollection = appIE.Document.getElementsByTagName
*appIE.Document.getElementById("txtNum").Value = "0000000"*
appIE.Document.getElementById("btnSearch").Click
End Sub
我将其更改为此,并且将元素放在iframe中效果很好
Sub GoToWebsiteTest2()
Dim appIE As InternetExplorerMedium
Dim objElement As Object
Dim objCollection As Object
Dim iframeDoc As MSHTML.HTMLDocument
Dim doc As MSHTML.HTMLDocument
Set appIE = New InternetExplorerMedium
sURL = "https://example.login.aspx"
With appIE
.Navigate sURL
.Visible = True
End With
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Set objCollection = appIE.Document.getElementsByTagName
'Set appIE = Nothing
appIE.Document.getElementById("txtUsername").Value = "user"
appIE.Document.getElementById("txtPassword").Value = "Pass"
appIE.Document.getElementById("btnLogin").Click
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
appIE.Navigate "https://example.Search.aspx"
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Set doc = appIE.Document
Set iframeDoc = doc.frames("EncJump").Document
iframeDoc.getElementById("txtNum").Value = "0000000"
iframeDoc.getElementById("btnSearch").Click
End Sub
答案 0 :(得分:3)
由于您无法发布HTML,因此猜测比什么都重要。有2个可能的问题:
因此,将出现问题的行更改为:
dim startTime as single
starttime = timer
on error resume next
do while appIE.Document.getElementById("txtNum") is nothing
if timer - starttime > 10 then 'Change 10 to # of max seconds to wait
msgbox "Element didn't load! Check internet connection, aborting"
exit sub
else
doevents
end if
loop
on error goto 0
appIE.Document.getElementById("txtNum").Value = "0000000"
以第1点为基础-您可以将iFrame视为自己的文档。通常,您会看到一个标记名<iframe>
。您将需要使用该对象的文档(我通常将其设置为等于iframe.contentdocument
),然后照常进行操作。