Web浏览器GetElementsByTagName在该标记区域内循环

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

我一直试图弄清楚如何做这件事已经有一段时间了。我想找到“live_”的表单类名,我可以使用下面的代码做得很好,但我不确定如何在该表单标记中获取文本值而不循环遍历整个代码并获取其他所有表单文本页面上的值。

我在winform上使用webbrowser控件。

我必须得到表单标记的代码是这样的:

Dim theElementCollection As HtmlElementCollection = Nothing

theElementCollection = wbNewsFeed.Document.GetElementsByTagName("form")

For Each curElement As HtmlElement In theElementCollection
   If curElement.GetAttribute("className").ToLower.Contains("live_") Then
     Dim theID As String = curElement.GetAttribute("data-live")
   End If
Next

上面的代码当前循环,直到它在该页面中找不到更多的表单标记。如果它找到了一个表单标签,那么它会查看该表单标签是否在其名称的任何部分中包含一个类名为live_的文件。此代码工作正常,并找到该类的所有表单标记。但是,某些表单标签仍然具有该类,但没有文本框,我也只想在该表单标签中搜索。

html看起来类似于:

<form class="live_574bf67566_58vvifkfkyu5237 commentable expand_mode" id="0_f" 
 onsubmit="return window.Event &amp;&amp;" action="change.php" method="post"
 data-ft='{"ge":"]"}' rel="async" data-live='{"seq":"574bf67566_1857067654230"}'>

   <input name="charset_test" type="hidden" value="6,52g,6b88">
   <input name="fb_dtsg" type="hidden" value="AQB4SLmU" autocomplete="off">
   [LOT of code here....]
   <input class="hiddenInput" type="hidden" autocomplete="off" data-id="785fgj67-774">
   <div class="innerWrap" data-reactid=".1l.1:4.0.$right.0.0.0.0.1.0.1">
      <textarea name="add_comment_text" title="Write a comment..." class="textInput mentions" placeholder="Write a comment..." value="Write a comment..." data-id="57-986-gn-52">Write a comment...</textarea>
   </div>
   [some more code here]
</form>

所以我的问题是:我如何只浏览当前的表单标签区域并查找它是否具有该文本框(.GetAttribute(“title”)。ToString.ToLower =“写评论......”)?

我尝试过以下方法:

Dim theElementCollection2 As HtmlElementCollection = Nothing

For Each curElement As HtmlElement In theElementCollection
   If curElement.GetAttribute("className").ToLower.Contains("live_") Then
      Dim theID As String = curElement.GetAttribute("data-live")

      theElementCollection2 = curElement.Document.GetElementsByTagName("textarea")

      For Each curElement2 As HtmlElement In theElementCollection2
         Debug.Print(curElement2.GetAttribute("title").ToLower.ToString)
         If curElement2.GetAttribute("title").ToLower.ToString = "write a comment..." Then
            Debug.Print("Found! " & curElement2.GetAttribute("name"))
         End If
      Next
   End If
Next

但这似乎只是遍及整个html页面仍然...

感谢您的时间和帮助!

vb.net dom webbrowser-control html-agility-pack getelementsbytagname
1个回答
0
投票

好像你需要:

curElement.Children.GetElementsByName("add_comment_text")(0)
© www.soinside.com 2019 - 2024. All rights reserved.