2016-01-13 56 views
0

我正在使用Excel-vba,并试图获取InternetExplorer对象的innerHTML,我的问题是点击链接后,innerHTML不会更改,并保持为第一个导航。IE点击链接后的innerHTML

有没有办法在点击链接后获取下一页的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

你试过在点击后等待就绪状态? – Thorarins

+0

是的,我以后试过,但没有任何事发生 – Jon777

回答

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进行调试。希望有所帮助。

+0

我添加了这段代码,但它运行不停,当我手动执行这个页面和标题立即改变。 – Jon777

+0

我编辑了答案,并把我的完整代码正在工作。我在运行之间手动关闭IE,因此每次都会创建一个新实例。进行适当的暂停是关键。很多时候我使用Wait命令而不是IE状态几秒钟。 – AMC08

+0

你尝试过吗?当我尝试它不起作用时,我也像你说的那样添加了Application.wait,但没有帮助。也许这取决于IE版本?我使用IE 11 – Jon777

0

我明白了,你是对的,它是一个计时问题,我发现如何在不冻结Excel的情况下调用innerHTML方法来获得正确的秒数,它使Excel等待新窗口超时20秒,否则返回“失败”。

这里是全码:

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 
+0

太棒了。每次我写这些IE脚本中的一个时,我认为......应该有更好的方式......但似乎并不存在。 – AMC08