Webbrowser GetElementsByTagName looping within that tag area

dom getelementsbytagname html-agility-pack vb.net webbrowser-control

Question

I have a been trying to figure out how to go about doing this for some time now. I am wanting to find the form classname of "live_" which i can do just fine with the below code but i am unsure how to go about getting a text value within that form tag without looping through the whole code and getting every other form text value on the page.

I am using a webbrowser control on my winform.

The code i have to get the form tag is this:

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

The code above currently loops until it finds no more form tags within that page. If it finds a form tag then it looks to see if that form tag contains a classname of live_ in any part of its name. This code works just fine and does find all the form tags by that class. However, some form tags still have that class but no text box that i am also wanting to search for within that form tag only.

The html looks similar to this:

<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>

So my question is: How do i go about looking through only that current form tag area and finding if it has that textbox (.GetAttribute("title").ToString.ToLower = "write a comment...")?

I've tried doing the following:

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

But that seems to only loop through the whole html page still...

Thanks for your time and help!

Popular Answer

Seems like you need:

curElement.Children.GetElementsByName("add_comment_text")(0)



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why