使用span id错误获取文本

时间:2016-12-18 14:35:01

标签: vb.net

我想从span id“hkoTemp”中获取文本 这是代码:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim wb As WebBrowser = New WebBrowser
    wb.Navigate(New Uri("http://www.hko.gov.hk/contentc.htm"))
    Dim temp = wb.Document.GetElementById("hkoTemp").GetAttribute("innerText")
    MsgBox(temp)
    End Sub


当我尝试运行它时,它会出现错误 Dim temp = wb.Document.GetElementById("hkoTemp").GetAttribute("innerText")


我该如何解决?

1 个答案:

答案 0 :(得分:0)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim wb As WebBrowser = New WebBrowser()
    wb.AllowNavigation = True
    AddHandler wb.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf wb_DocumentCompleted)
    wb.Navigate(New Uri("http://www.hko.gov.hk/contentc.htm"))
End Sub

Private Sub wb_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
    ' wb.Document is not null at this point
    Dim wb As WebBrowser = sender
    Dim temp = wb.Document.GetElementById("hkoTemp").GetAttribute("innerText")
    MsgBox(temp)
End Sub