views:

873

answers:

5

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.

A: 

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");
Obalix
A: 

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");
Darin Dimitrov
Sorry, this does not work as the `select` element does not have a `value` attribute. Just tested it ...
Obalix
Thanks setting the attribute worked.
CWinKY
+1  A: 

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

mattb
+1  A: 
HtmlElement hField = webBrowser1.Document.GetElementById("ID");  
hField.SetAttribute("selectedIndex", "2");  

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

Tom
A: 
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");
                    }
                }
alhambraeidos