单击WebBrowser控件中的按钮

问题描述 投票:0回答:2

HTML Body,Ty求助我。

<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没有很好的经验.................................. ....

html vb.net dom getelementsbytagname
2个回答
0
投票

试试这个 :

"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

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


0
投票

看起来我也不是专家,但是如果你想为按下的按钮设置一个“活动”类,那么你应该在大多数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指令将此代码优化为一行。

© www.soinside.com 2019 - 2024. All rights reserved.