我使用Excel-vba,并尝试获取InternetExplorer对象的innerHTML,我的问题是,在点击链接后,innerHTML不会改变并保持为第一个导航。 / p>
有没有办法在点击链接后获取下一页的innerHTML? 这是代码:
Dim ie As InternetExplorer
Dim hd As HTMLDocument
Set ie = New InternetExplorer
ie.navigate "http://www.stormchaser.niu.edu/machine/textfcstsound.html"
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set hd = ie.document
hd.all.Item("mo")(4).Checked = True
hd.all.Item("ft")(11).Checked = True
Set objInputs = ie.document.getElementsByTagName("input")
For Each ele In objInputs
If ele.Type = "submit" Then
ele.Click
End If
Next
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
hd.Links(0).Click
Range("K4") = ie.document.body.innerHTML
我非常感谢你的帮助。
乔恩
答案 0 :(得分:0)
它看起来像一个计时问题,我发现这是自动化IE中一个非常常见的问题。在执行hd.Links(0)之前,代码不等待加载以下页面。点击这样它实际上是单击旧页面上的链接。我测试了以下代码,似乎运行正常。它使用页面标题作为检查是否加载而不是ie.ReadyState似乎没有工作。
Sub test()
Dim ie As InternetExplorer
Dim hd As HTMLDocument
Set ie = New InternetExplorer
ie.Navigate "http://www.stormchaser.niu.edu/machine/textfcstsound.html"
ie.Visible = True
Do While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set hd = ie.Document
hd.all.Item("mo")(4).Checked = True
hd.all.Item("ft")(11).Checked = True
Set objInputs = ie.Document.getElementsByTagName("input")
For Each ele In objInputs
If ele.Type = "submit" Then
ele.Click
'Debug.Print ele.outerHTML
End If
Next
Do While hd.Title <> "Forecast Sounding Text Output for KOKC" ' ie.ReadyState <> READYSTATE_COMPLETE
'Debug.Print hd.Title, hd.Links(0)
'DoEvents
Loop
'While ie.ReadyState <> READYSTATE_COMPLETE: Wend
Set hd = ie.Document
Debug.Print hd.Title, hd.Links(0)
hd.Links(0).Click
'Do While ie.ReadyState <> READYSTATE_COMPLETE
' DoEvents
'Loop
While ie.ReadyState <> READYSTATE_COMPLETE: Wend
Do While hd.Title <> "" ' ie.ReadyState <> READYSTATE_COMPLETE
Loop
Debug.Print hd.Title, hd.body.innerHTML
Range("K4") = hd.body.innerHTML
End Sub
我还建议ie.Visible进行调试。希望有所帮助。
答案 1 :(得分:0)
我明白了,你是正确的,这是一个时间问题,我发现如何在没有冻结Excel的情况下抓住正确的第二个调用innerHTML方法,它给Excel提供了20秒的超时等待新窗口,否则返回&#34;失败&#34;。
这是完整的代码:
Set ie = New InternetExplorer
ie.navigate "http://www.stormchaser.niu.edu/machine/textfcstsound.html"
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set hd = ie.document
T1 = ie.document.Title
hd.all.Item("mo")(4).Checked = True
hd.all.Item("ft")(10).Checked = True
hd.getElementById("stn").Value = "LLBG"
Set objInputs = ie.document.getElementsByTagName("input")
For Each ele In objInputs
If ele.Type = "submit" Then
ele.Click
End If
Next
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
T2 = Now
Do While (Now < T2 + TimeValue("0:00:20")) And ie.document.Title = T1
DoEvents
Loop
If ie.document.Title = T1 Then
Debug.Print "failed"
GoTo end_ext
End If
T1 = ie.document.Title
ie.document.Links(0).Click
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
T2 = Now
Do While (Now < T2 + TimeValue("0:00:20")) And ie.document.Title = T1
DoEvents
Loop
If ie.document.Title = T1 Then
Debug.Print "failed"
GoTo end_ext
Else
'Debug.Print "succeeded"
End If
Debug.Print ie.document.body.innerText
end_ext:
ie.Quit
Set hd = Nothing
Set ie = Nothing