【问题标题】:Find and Click cell by inner html string inside table Webbrowser通过表格Webbrowser中的内部html字符串查找并单击单元格
【发布时间】:2016-09-06 06:38:16
【问题描述】:

我正在尝试在第 1 列中查找字符串并自动单击第 3 列中的“详细信息”..

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

直到最近我才能使用此代码找到第 1 列中的单词:

    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

现在我必须想办法点击第 3 列中的单词 detail.. 这可能吗?

【问题讨论】:

    标签: vb.net winforms webbrowser-control


    【解决方案1】:

    找到您要查找的单元格后,只需迭代该行中的其余单元格,直到找到 "detail" 单元格。

    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
    

    或者,如果单击单元格本身不起作用,请单击第一个 a 标记:

    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
    

    【讨论】:

    • 我使用您的主要解决方案,但它不起作用.. 然后我将您的代码“For Each rcell As HtmlElement In row.all”更改为“For Each rcell In row.all”和“rcell. InnerText.Contains("detail")" 到 "rcell.InnerText("detail")".. 它现在可以工作了.. 无论如何谢谢.. 我必须标记你的答案吗?
    • @CrazyThink :老实说,您的更改没有多大意义。您的第一个更改没有任何区别,第二个更改甚至不应该编译。将我的代码从 InnerText 更改为 InnerHtml 可以工作。 - 如果它有效,那么我建议您将其标记为已接受的答案,是的。这可以通过按我的答案左侧的复选标记来完成。
    • @CrazyThink :我在回答中将InnerText 更改为InnerHtml
    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 2018-07-23
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    相关资源
    最近更新 更多