2017-02-01 15 views
0

我正在嘗試使用VBA導航到網頁,填寫一些信息,單擊按鈕,移到下一頁並收集一些結果信息。這是我遇到的問題:代碼經歷了前幾個步驟,但是當它試圖解析第二個頁面的html時,它會從第一個頁面返回HTML。出於某種原因,即使屏幕上顯示了正確的頁面(具有所需的信息),WebBrowser.Document中的HTML也不會隨着新的屏幕加載而改變。我試過強迫我等待頁面加載,但這似乎沒有任何區別。代碼如下:VBA:從導航到的頁面獲取HTML數據通過點擊

Sub SnowLoad(Latitude As String, Longitude As String, State As String) 


Dim MyHTML_Element As IHTMLElement 
Dim HTMLDoc As HTMLDocument 
Dim MyURL As String 
Dim SnowTag As String 
Dim SnowPos As Long 
SnowTag = "Load" 
On Error GoTo Err_Clear 
MyURL = "http://snowload.atcouncil.org/" 
''open new explorer 
Dim MyBrowser As New InternetExplorer 
MyBrowser.Silent = True 
''navigate to page 
MyBrowser.navigate MyURL 
MyBrowser.Visible = True 
''wait until ready 
Do 
DoEvents 
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE 

Set HTMLDoc = MyBrowser.document 
HTMLDoc.all.optionCoordinate_LATLON.Click 

HTMLDoc.all.coordinate_lat.Value = Latitude 
HTMLDoc.all.coordinate_lon.Value = Longitude 
Set elems = HTMLDoc.getElementsByTagName("button") 
    For Each e In elems 

     If (e.getAttribute("class") = "btn") Then 
      e.Click 
      Exit For 
     End If 

    Next e 

Do 
DoEvents 
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE 

Set elems = HTMLDoc.getElementsByTagName("p") 

    For Each e In elems 
     Debug.Print e.innerHTML 
     If InStr(e.innerHTML, SnowTag) Then 
      SnowPos = InStr(e.innerHTML, SnowTag) 
      Range("SnowPosition").Value = SnowPos 
      Exit For 
     End If 

    Next e 

Err_Clear: 
If Err <> 0 Then 
Err.Clear 
Resume Next 
End If 

End Sub 

我對於我的生活無法弄清楚如何獲取第二頁上顯示的信息。似乎沒有任何數量的谷歌搜索可以產生答案或類似的問題。它可能與使用按鈕導航代替實際調用.navigate有關嗎?

+0

我們可以看到HTML?這聽起來像是兩件事之一,即使你的代碼頁面實際上並未完全加載。新頁面位於框架/ iFrame中。或者別的什麼,看到網站或HTML應該有所幫助。 –

+0

我似乎無法弄清楚如何將正確格式化的HTML轉換爲原始文章(當我粘貼時它會變成火車殘骸)。這裏是網站: [雪鏈接](http://snowload.atcouncil.org/index.php/component/vcpsnowload/item) 我不熟悉HTML,所以我不完全確定是否任何一個值得注意。 – Tsnorthern

+0

您是否試圖返回特定區域的降雪的psf? –

回答

1

我已經看到這個工作可靠。我只是跑了5-6次沒有問題。在頁面刷新後,我也沒有找到元素。爲了解決這個問題,我在加載後得到了一個全新的對象引用。這似乎運作良好。

此外,我在代碼上進行了一般清理。

#If VBA7 Then 
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 
#Else 
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 
#End If 

Sub SnowLoad() 
On Error GoTo errhand: 
    'I hard coded these for testing 
    Latitude = "46" 
    Longitude = "-87" 

    Dim MyURL  As String: MyURL = "http://snowload.atcouncil.org/" 
    Dim element  As Object 

    ''open new explorer, I used late binding 
    Dim MyBrowser As Object: Set MyBrowser = CreateObject("InternetExplorer.Application") 
    MyBrowser.Visible = True 
    MyBrowser.navigate MyURL 

    'Wait for the browser to finish loading 
    waitForLoad MyBrowser 

    With MyBrowser.document 
     .getElementByID("optionCoordinate_LATLON").Click 
     .getElementByID("coordinate_lat").Value = Latitude 
     .getElementByID("coordinate_lon").Value = Longitude 
     .getElementsByName("btn-submit")(0).Click 
    End With 

    waitForLoad MyBrowser 

    'For whatever reason the HTML isn't updating along with the page 
    'Instead, I'm just getting an updated reference to the IE object with the 
    'below function, weird issue, but this seems to work 
    Set MyBrowser = FindWindow("component/vcpsnowload/item") 
    Set element = MyBrowser.document.Forms("adminForm").getelementsByTagName("p")(0) 
    Range("A1").Value = element.innertext 

    Exit Sub 
errhand: 
    MsgBox (Err.Number & " " & Err.Description) 
End Sub 

Public Function FindWindow(SearchCriteria As String) As Object 
    Dim window As Object 

    For Each window In CreateObject("Shell.Application").Windows 
     If window.locationurl Like "*" & SearchCriteria & "*" Then 
      Set FindWindow = window 
      Exit Function 
     End If 
    Next 

    Set FindWindow = Nothing 
End Function 

Public Sub waitForLoad(ByVal IE As Object) 
    Dim i As Byte 
    Sleep 500 ' wait a bit for the page to start loading 
    Do 
     i = i + 1 
     Sleep 500 
    Loop Until IE.readystate = 4 Or IE.busy = False Or i >= 20 
End Sub 

上面的代碼返回此:Any elevation: Ground Snow Load is 60 psf

+0

沒問題,很高興我能幫上忙。真的很奇怪的問題,據我所知,它不應該那樣做。刷新完成後,HTML應該更新。 –