Reputation:
Given the following html in a Winforms Webbrowser DOM, I'm attempting to get the second element with value="type102"
<div class="Input"><input type="radio" name="type" value="type101"
onclick="setType('type101');"><a href="javaScript:setType('type101');"
onclick="s_objectID="javascript:setType('type101');_1";return this.s_oc?
this.s_oc(e):true">type101</a></div>
<div class="Input"><input type="radio" name="type" value="type102"
onclick="setType('type102');" checked="checked"><a href="javaScript:setType('type102');"
onclick="s_objectID="javascript:setType('type102');_1";return this.s_oc?
this.s_oc(e):true">type102</a></div>
I've used
HtmlElement htmlElem = browser.Document.GetElementById(....
and
HtmlElement htmlElem = browser.Document.All.GetElementsByName(....
before, but in this instance they are both the same so would need to get by value or href
Is it possible to get the second element directly without external libraries or would I have to get a collection of GetElementsByName and iterate through them?
Upvotes: 3
Views: 14707
Reputation: 1197
HtmlElementCollection col= webBrowser1.Document.GetElementsByTagName("input");
HtmlElement wanted;
foreach (HtmlElement item in col)
{
if (item.GetAttribute("value")=="type102")
{
wanted = item;
break;
}
}
Upvotes: 3
Reputation: 2261
var elems = webBrowser1.Document.GetElementsByTagName("input");
var myInputElement = elems.First(e => !string.IsNullOrEmpty(e) && e.Trim().ToLower().Equals("type102"));
foreach (var elem in elems)
{
var value = elem.GetAttribute("value");
if (string.IsNullOrEmpty(value) && value.Trim().ToLower().Equals("type102"))
{
MessageBox.Show(value);
break;
}
}
Or to make it shorter
var elems = webBrowser1.Document.GetElementsByTagName("input");
var myInputElement = elems.First(e => !string.IsNullOrEmpty(e) && e.Trim().ToLower().Equals("type102"));
Is this what you are looking for? you can also have a look at HtmlAgilityPack for things like these.
Upvotes: 2
Reputation: 2754
HtmlElement htmlElem = browser.Document.All.GetElementsByName(...)[1]
Upvotes: 1