Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get html element by value

Tags:

c#

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=&quot;javascript:setType('type101');_1&quot;;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=&quot;javascript:setType('type102');_1&quot;;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?


1 Answers

HtmlElementCollection col=  webBrowser1.Document.GetElementsByTagName("input");
HtmlElement wanted;

foreach (HtmlElement item in col)
{
    if (item.GetAttribute("value")=="type102")
    {
        wanted = item;
        break;
    }
}
like image 79
aliassce Avatar answered Aug 17 '26 22:08

aliassce