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.
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");
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With