Hi Guys

Can anyone give me an example on how to retrieve the value 16 in bold under the tagname <td>In process</td> with a webbrowser and insert this value to a texbox. Thanks

<tr class="odd">
<td>In Process</td>
<td class="number">***16***</td>
<td class="number">1</td>
<td class="number">01:16:48</td>
<td class="number">02:54:00</td>
<td class="radio"><input type="radio" name="queue" checked="checked" onclick="document.location.href='management.lhtml?queue=1';" /></td>
<td class="number">11</td>
<td class="number">0</td>
<td class="number">00:59:24</td>
<td class="number">07:23:26</td>
<td class="radio"><input type="radio" name="queue" onclick="document.location.href='management.lhtml?queue=2';" /></td>
<tr class="odd">
<td class="number">20</td>
<td class="number">0</td>
<td class="number">03:26:40</td>
<td class="number">08:38:42</td>
<td class="radio"><input type="radio" name="queue" onclick="document.location.href='management.lhtml?queue=3';" /></td>

To retrieve the value there must be a pattern that can be implemented, let us assume that the pattern is as follows.

  1. There is only one table row with where the first child element has it's innner text equal to "In Process".
  2. The "In Process" element has at least one sibling and it can be parsed as a number.

Essentially, for this to work reliably, the HTML must be layedout like you have shown. Using these constraints, it is possible to construct a query of the document to attempt to retrieve a number. Additional constrainst could be imposed, but I'll leave that to you.

The following code demonstrates how to query a webbrowser's document and attempt to return a number based on the constraints defined above.

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

     Dim value As Single
     If GetMyValue(wb1, value) Then
        MsgBox("the value is: " & value.ToString)
        MsgBox("could not retrieve value")
     End If

  End Sub

  Private Function GetMyValue(ByVal wb As WebBrowser, ByRef value As Single) As Boolean
     If wb.Document Is Nothing Then Return False 'no point in going any further

     Dim rows As List(Of HtmlElement) = (From tr In wb.Document.GetElementsByTagName("TR") Select tr Where CType(tr, HtmlElement).FirstChild.InnerText = "In Process").Cast(Of HtmlElement).ToList
     If rows.Count = 1 Then
        Dim firstElementAfterInProcess As HtmlElement = rows(0).FirstChild.NextSibling

        If firstElementAfterInProcess IsNot Nothing Then
              value = Single.Parse(firstElementAfterInProcess.InnerText)
              Return True
           Catch ex As Exception
              'could not parse a value
              Return False
           End Try
           Return False

        End If 'firstElementAfterInProcess IsNot Nothing
        Return False
     End If ' rows.Count = 1

  End Function
This article has been dead for over six months. Start a new discussion instead.