CrazyThink CrazyThink - 2 months ago 28
Vb.net Question

Find and Click cell by inner html string inside table Webbrowser

iam trying to find string in collumn 1 and automaticaly click "detail" in collumn 3..

|-----------------------------------------|
| Collumn 1 | Collumn 2 | Collumn 3 |
|-----------------------------------------|
| David | 12345 | detail |
|-----------------------------------------|
| Jhon | 67890 | detail |
|-----------------------------------------|
| Ester | 67890 | detail |
|-----------------------------------------|


until recently I could only find the words in column 1 using this code :

Dim tables As HtmlElementCollection = Me.WebBrowser.Document.GetElementsByTagName("table")
For Each tbl As HtmlElement In tables
For Each row As HtmlElement In tbl.All
For Each cell As HtmlElement In row.All
If Not cell.Style Is Nothing Then
If cell.InnerText.Contains("Jhon") Then
cell.InvokeMember("click") '---Not working because "Jhon" is not Hyperlink.
End If
End If
Next
Next
Next


Now I have to find ways to click on the word detail in collumn 3.. is that possible?

Answer

Once you've found the cell you're looking for just iterate the rest of the cells in that row until you find the "detail" cell.

If cell.InnerText.Contains("Jhon") Then
    For Each rcell As HtmlElement In row.All
        If rcell.InnerHtml.Contains("detail") Then
            rcell.InvokeMember("click")
            Exit For
        End If
    Next
End If

Alternatively, if clicking the cell itself does not work, click the first a tag:

If cell.InnerText.Contains("Jhon") Then
    For Each rcell As HtmlElement In row.All
        If rcell.InnerHtml.Contains("detail") Then
            Dim tags As HtmlElementCollection = rcell.GetElementsByTagName("a")
            If tags.Length > 0 Then
                tags(0).InvokeMember("click")
            End If
            Exit For
        End If
    Next
End If
Comments