Pulling data off a web page populated through a webbrowser control

xzibited2

Active member
Joined
Jul 9, 2008
Messages
26
Programming Experience
1-3
Morning- I have a webbrowser control that populates a specific page when loaded. That page has two textbox fields that I need to pull data out of and use for another portion of the application. An example of the website is:

How Far is it Between 97223 and 97006

The two text boxes I'm trying to pull are right below the map (labeled "Distance as the Crow Flies" and "Distance by Land Transport").

Any thoughts on how to get this done? I've run out of ideas...
 
VB.NET:
Me.Label1.Text = Me.WebBrowser1.Document.All.GetElementsByName("pointa")(0).GetAttribute("Value")
 
I was able to pull the two locations using that code, but still cannot pull the final distances (they are the two text boxes next to the labels "Distance as the Crow Flies" and "Distance by Land Transport").
 
As you can see in source code these elements have id "distance" and "transport" so you use .Document.GetElementById method.
 
I gave that a shot, and it's coming up as 0.000 every time. It's assigned the value of '0.000' even if there is a different value in the box.

VB.NET:
Me.distance.Text = Me.WebBrowser1.Document.GetElementById("distance").GetAttribute("value")
 
That is weird, I get the values correctly with same code. When exactly do you call this code? Notice that these fields are populated with client code after page is loaded, so they are not ready for DocumentCompleted event.
 
I think that may be the problem, I'm calling it in the documentcompleted event. When should this be called? I really appreciate all your help with this.
 
You can for example attach to the onpropertychange html event of this element like this:
VB.NET:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
    If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        Dim el As HtmlElement = Me.WebBrowser1.Document.GetElementById("distance")
        el.AttachEventHandler("onpropertychange", AddressOf DistanceChanged)
    End If
End Sub

Private Sub DistanceChanged(ByVal sender As Object, ByVal e As EventArgs)
    Me.Distance.Text = Me.WebBrowser1.Document.GetElementById("distance").GetAttribute("value")
End Sub
 
Back
Top