在div中单击按钮一次而不是所有div

时间:2015-02-22 07:36:21

标签: vb.net

基本上我想点击这个"按钮"只有一次在div" test1-header" (此按钮出现在每个div中,并且没有名称或ID,但我可以通过他的班级点击它)

<div class="test1-header">
 <td class="button">  '' only this button i want to click
<div class="test2-header">
 <td class="button">
<div class="test3-header">
 <td class="button">

但是当我执行我的代码时,它会点击页面上所有3个div中的所有按钮。

基本上这是我的行动代码:

Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
        For Each webpageelement As HtmlElement In allelements
            If webpageelement.GetAttribute("className") = "button" Then
                webpageelement.InvokeMember("click")
            End If
        Next

我希望我可以将这些div或其他东西分开。请帮助一下:D

//编辑 - 这个代码我根据Crowcoder的说法进行了修改,但它不起作用:(

Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

    For Each webpageelement As HtmlElement In allelements
        If webpageelement.GetAttribute("data-aid") = "115553109754009608849"   Then
            For Each elem As HtmlElement In webpageelement.Children
                If elem.GetAttribute("className") = "mod-button" Then
                    elem.InvokeMember("click")
                    Exit For
                End If
            Next
        End If
    Next

//编辑 - 好的,这就是我正在处理的HTML,以及按钮的所有父div

<html>
 <body>
  <div id=""body-container"">
   <div id=""page-container"">
    <div id=""content"">
     <div class=""branded-page-v2-container branded-page-base-bold-titles branded-page-v2-container-flex-width branded-page-v2-secondary-column-hidden"">
      <div class=""branded-page-v2-col-container"">
       <div class=""branded-page-v2-col-container-inner"">
        <div class=""branded-page-v2-primary-col"">
         <div class=""   yt-card  clearfix"">
          <div class=""branded-page-v2-primary-col-header-container branded-page-v2-primary-column-content"">
           <div class=""branded-page-v2-body branded-page-v2-primary-column-content"">
            <div id=""watch-response"">
             <div id=""watch-response-content"">
              <div id=""watch-discussion"">
               <div id=""comments embedded"">
                <div class=""comments-list"">
                 <div class=""comment-entry"">
                  <div data-aid=""101515802118596397194"">   ''<-- this is a unique value of it's closest parent div
                   <div class=""content"">
                    <div class=""comment-footer"">
                     <div class=""comment-footer-actions"">
                      <table class=""footer-button-bar"">
                       <tbody>
                        <tr>
                         <td class=""mod-button"">  ''<-- this is the button i'd like to push
                         </td>
                        </tr>
                       </tbody>
                      </table>
                     </div>
                    </div>
                   </div>
                  </div>
                 </div>
                </div>
               </div>
              </div>
             </div>
            </div>
           </div>
          </div>
         </div>
        </div>
       </div>
      </div>
     </div>
    </div>
   </div>
  </div>
 </body>
</html>

1 个答案:

答案 0 :(得分:0)

您的HTML无效。你的div没有关闭,你不能把td作为div的直接孩子。我不知道该标记将如何表现,但通常你要做的是找到你想要的元素最接近的父元素,如果你想要的元素在某种程度上不是唯一的。然后你检查孩子们,希望在父母的上下文中你想要的元素是独一无二的。 这是一个用更好的HTML演示这种技术的例子:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    WebBrowser1.DocumentText = "<html><body><div class=""test1-header""><div class=""button"" onclick=""alert('foo')""></div> test </div></body></html>"

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

    For Each webpageelement As HtmlElement In allelements
        If webpageelement.GetAttribute("className") = "test1-header" Then
            For Each elem As HtmlElement In webpageelement.Children
                If elem.GetAttribute("className") = "button" Then
                    elem.InvokeMember("click")
                    Exit For
                End If
            Next
        End If
    Next
End Sub