vb.net Webbrowser GetElementsByTagName 在该标签区域内循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22675868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webbrowser GetElementsByTagName looping within that tag area
提问by StealthRT
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 codebut i am unsure how to go about getting a text value within that form tagwithout looping through the whole code and getting every other form text value on the page.
一段时间以来,我一直试图弄清楚如何去做这件事。我想找到“live_”的表单类名,我可以用下面的代码做的很好,但我不确定如何在不遍历整个代码并获取所有其他表单文本的情况下获取该表单标签中的文本值页面上的值。
I am using a webbrowser control on my winform.
我在我的 winform 上使用 webbrowser 控件。
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.
上面的代码当前循环,直到在该页面中找不到更多的表单标签。如果它找到一个表单标签,那么它会查看该表单标签是否在其名称的任何部分中包含类名live_。这段代码工作得很好,并且确实找到了该类的所有表单标签。但是,某些表单标签仍然具有该类,但没有我也只想在该表单标签中搜索的文本框。
The html looks similar to this:
html 看起来类似于:
<form class="live_574bf67566_58vvifkfkyu5237 commentable expand_mode" id="0_f"
onsubmit="return window.Event &&" 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 onlythat current form tag area and finding if it has that textbox (.GetAttribute("title").ToString.ToLower = "write a comment...")?
所以我的问题是:我如何只查看当前的表单标记区域并查找它是否具有该文本框(.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...
但这似乎只循环遍历整个 html 页面仍然......
Thanks for your time and help!
感谢您的时间和帮助!
回答by shmosel
Seems like you need:
好像你需要:
curElement.Children.GetElementsByName("add_comment_text")(0)

