Question

I'm trying to automate in a WinForm using a WebBrowser control to navigate and pull report info from a website. You can enter values in textboxes and invoke the click events for buttons and links, but I have not figured out how select a option drop-down .... in a automated way. Anybody recommend how to select a item from a drop-down, given this html example:

<SELECT id="term_id" size="1" name="p_term_in"><option value="">Select Another Term<option value="201050">Summer 2010<option value="201010">Spring 2010<option value="200980">Fall 2009</SELECT>

For others that can learn from entering values to textboxes and invoking click events here's how you do it:

webBrowser1.Document.GetElementById("<HTML ELEMENT NAME>").SetAttribute("value", "THE NAME");

Invoke button or hyperlink click:

webBrowser1.Document.GetElementById("<BUTTON>").InvokeMember("click");

So I've solved entering values and invoking click, but I have not solved selecting a drop-down value.

Was it helpful?

Solution

Assuming you have the following select in the HTML:

<select id="term_id" size="1" name="p_term_in">
    <option value="">Select Another Term
    <option value="201050">Summer 2010
    <option value="201010">Spring 2010
    <option value="200980">Fall 2009
</select>

This should allow you to preselect the third value:

webBrowser1.Document.GetElementById("term_id").SetAttribute("value", "201010");

OTHER TIPS

var select = webBrowser.Document.GetElementById("ddlProyectos");

mshtml.HTMLSelectElement cbProyectos = select.DomElement as mshtml.HTMLSelectElement;

var total = cbProyectos.length;
for (var i= 0; i < total; i++)
{
    cbProyectos.selectedIndex = i;
    if (cbProyectos.value.Contains("13963"))
    {
        break;
    }

}
//cbProyectos.selectedIndex = 4;
select.InvokeMember("onchange");

select.Children[4].SetAttribute("selected", "selected");

var theElementCollection = webBrowser.Document.GetElementsByTagName("select");
foreach (HtmlElement el in theElementCollection)
{
    if (el.GetAttribute("value").Equals("13963"))
    {
        el.SetAttribute("selected", "selected");
        //el.InvokeMember("click");
    }
}

You will have to select the selected attribute on the option you want.

Given:

<select id="mySelect">
  <option>1</option>
  <option>2</option>
  <option>3</option>
</select>

The following would selct the third option:

webBrowser1.Document
           .GetElementById("")
           .Children.GetElementsByName("option")[2]
           .SetAttribute("selected", "selected");

try this:

add reference to microsoft.mshtml in project --> add reference...

    Dim cboTemp As mshtml.HTMLSelectElement
    cboTemp = WebBrowser1.Document.GetElementById("myselect").DomElement
    cbotemp.selectedindex = 2

having the variable cbotemp set to a select element gives you greater access to the control :)

HtmlElement hField = webBrowser1.Document.GetElementById("ID");  
hField.SetAttribute("selectedIndex", "2");  

select by index (zero based) not the value....

I'm answering on this post after five years, for the people who are searching a solution of this problem.

If you just need to submit/post a value for the dropdown then this line is sufficient:

webBrowser1.Document.GetElementById("term_id").SetAttribute("value", "200980");

But if you really need to select an underlying OPTION, then:

HtmlElement selectDom = webBrowser1.Document.GetElementById("term_id");
foreach (HtmlElement option in selectDom.GetElementsByTagName("option"))
{
    if (option.GetAttribute("value") == "200980")
    {
        var dom = option.DomElement as dynamic;
        dom.selected = true;
        // selectDom.InvokeMember("onChange"); // if you need this too
        break;
    }
}

You can use this:

webBrowser1.Document.GetElementById("term_id").SetAttribute("value",yourText); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top