【问题标题】:Getting the ID of a class name tag in webbrowser在 webbrowser 中获取类名标签的 ID
【发布时间】:2014-07-12 07:59:06
【问题描述】:

所以我得到了一个带有输入标签的网页

<div id="mainID" class="outside">
   <div class="inside">
      <input id="5443" name="43" type="checkbox" class="classname" /> 
      <input id="5444" name="44" type="checkbox" class="classname" /> 
      <input id="5445" name="45" type="checkbox" class="classname" /> 
      <input id="5446" name="46" type="checkbox" class="classname" /> 
      <input id="5447" name="47" type="checkbox" class="classname" /> 
   </div>
</div>

ID 和名称各不相同,但类名始终相同 - 所以使用类标签(类名),我如何检索 ID 名称,5443(在 for 循环中一一列出)使用 Visual Basic 浏览器?我不想要名称标签信息。

编辑:添加代码

【问题讨论】:

    标签: html vba dom web-scraping


    【解决方案1】:

    CSS 选择器:

    如果您使用的是 VBA,那么一旦您将 HTML 包含在 HTMLDocument 中,例如

    IE.document,那么你可以使用documentquerySelectorAll方法应用CSS selector来获取感兴趣的元素。

    要应用的 CSS 选择器是:#mainID div.inside input

    这表示在 id 为 mainID 的 div 内的 className inside 内有 input 标签的地方获取元素。 "#" 是 id,"." 是 className。 " " 代表嵌套。


    CSS 查询:

    HTML 上的选择器返回以下元素


    VBA:

    您可以使用以下内容检索元素,然后使用 Split 解析 HTML 以获取其中的 IdsquerySelectorAll 返回一个 nodeList 您循环的长度。您不能使用For Each Loop。它会使 Excel 崩溃!

    Option Explicit
    Public Sub GetInfo()
        Dim aNodeList As Object, i As Long
        'Other code to get the HTMLDocument.......
        Set aNodeList = HTMLDocument.querySelectorAll("#mainID div.inside  input")
    
        For i = 0 To aNodeList.Length - 1
            Debug.Print GetId(aNodeList(i).innerHTML)
            'Debug.Print GetId(aNodeList.item(i).innerHTML) '<== Sometimes the syntax is like this rather than the above
        Next i
    End Sub
    
    Public Function GetId()
        GetId = Replace$(Split(Split(s, "id=")(1), Chr$(32))(0), Chr$(34), vbNullString)
    End Function
    

    【讨论】:

      【解决方案2】:

      我在查看 stackoverflow.com 上的原始帖子后发现了这一点

      Dim theElementCollection As HtmlElementCollection = Nothing
              theElementCollection = WebBrowser1.Document.GetElementsByTagName("div")
              For Each curElement As HtmlElement In theElementCollection
                  'If curElement.GetAttribute("classname").ToString = "example"  It doesn't work.  
                  ' This should be the work around.
                  If InStr(curElement.GetAttribute("classname").ToString, "yourcalssname") Then
                      ' Doesn't even fire.
                      ' InvokeMember(test) after class is found.
                      MessageBox.Show(curElement.GetAttribute("InnerText"))
      
                  End If
              Next
      

      【讨论】:

      • 很遗憾没有工作。我知道其中的笑话,并试图搞乱与以前类似的东西,但没有奏效。我忘了添加代码,我现在添加。
      【解决方案3】:

      在后端这不是一件容易的事。但是客户端通过 jquery 很容易。

      这是您在 c# 中答案的链接。

      How to select an element by Class instead of ID in ASP.NET?

      “你不能只根据类名来做。类名和id是完全无关的。想想看:同一个类可以用于不同的对象,但id值在页面上应该始终是唯一的。所以,这个请求根本没有意义;答案很明显。”参考链接。

      http://www.codeproject.com/Questions/720610/csharp-code-for-get-ID-of-the-control-using-its-cl

      C# how to get the name (in string) of a class property?

      下面这个例子是通过 jquery 的 clint 端。很简单。

      在浏览器端使用 jquery 或 javascript。在你的情况下使用 jquery。

      在您的 HTML 页面中添加最新的 jquery 并将脚本标记中的代码编写为

      <script>
        $( document ).ready(function() {        
              $('.test').click(function() {
                     alert( this.id );
              });
          });
      </script>
      

      参考链接:-jquery: get id from class selector

      您可以从品种条件进行搜索

      Get the ID by Class name JQuery

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-30
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多