2016-01-13 57 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