单击 WebBrowser 控件中的按钮

Click on a button in a WebBrowser control

HTML Body, 帮帮我。

<div class="bottoms_list">
    <div class="One cell">
        <button class="active"><span><i class="Picture"></i></span>1</button>
    </div>
    <div class="Two cell">
        <button class=""><span><i class="Picture"></i></span>2</button>
    </div>
    <div class="three cell">
        <button class=""><span><i class="Picture"></i></span>3</button>
    </div>
    <div class="four cell">
        <button class=""><span><i class="Picture"></i></span>4</button>
    </div>
</div>

此处按钮 (1) 处于活动状态。

假设我想激活按钮 (2)。

我试过了:

Dim spanpic As String = ""

For Each choice As HtmlElement In Form1.WebBrowser1.Document.GetElementsByTagName("div")

    If choice.GetAttribute("className").Contains("bottoms_list") AndAlso choice.Id.StartsWith("2") Then
        spanpic = choice.Id

        For Each elm As HtmlElement In choice.Document.GetElementsByTagName("button")

            If elm.GetAttribute("className") = "button" Then
                elm.InvokeMember("click")
                Exit For
            End If
        Next
    End If
Next

 '''''''''''''''''
Threading.Thread.Sleep(100)

 Try

 For Each elm As HtmlElement In 
 Form1.WebBrowser1.Document.GetElementById(spanpic).Document.GetElementsByTagName("button")

If elm.GetAttribute("className") = "bottoms_list" Then
                If elm.GetAttribute("disabled") = "disabled" Then
                Else
                    Exit For
                End If
            End If
        Next
    Catch
    End Try

这就是我所知道的,我对元素 id 没有很好的经验................................. .........

你看我也不是专家,但是如果你想给按下的按钮设置一个class "active",那么你应该在大多数情况下使用class绑定技术java-脚本框架。

对我来说,我使用 VueJs,我会这样做:

HTML正文:

<div id="myList">
  <div class="bottoms_list">
    <div class="One cell">
      <button @click="isActive = 1" :class="{active: isActive == 1}"><span><i class="Picture"></i></span>1</button>
    </div>
    <div class="Two cell">
      <button @click="isActive = 2" :class="{active: isActive == 2}"><span><i class="Picture"></i></span>2</button>
    </div>
    <div class="three cell">
      <button @click="isActive = 3" :class="{active: isActive == 3}"><span><i class="Picture"></i></span>3</button>
    </div>
    <div class="four cell">
      <button @click="isActive = 4" :class="{active: isActive == 4}"><span><i class="Picture"></i></span>4</button>
    </div>
  </div>
</div>

那么您的 Vue 实例应该如下所示:

new Vue({ 
  el: '#myList',
  data: {
    isActive: 0,
  },
});

您可以使用 v-for 指令将此代码优化为一行。

试试这个:

"get collection of all div in the webpage"
For Each divSect As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")

    "get the div which has the tag of Two Cell"
    If divSect.OuterHtml.Contains("Two Cell") then

        "get the button inside the div which has the tag of Two Cell"
        For Each elem as HtmlElement In divSect.children
        If elem.GetAttribute("className") = "button" Then
            elem.InvokeMember("click")
        End if
        Next
    End if    
Next

希望这些代码可以解决您的问题。