WebBrowser control HTMLDocument automate selecting option drop-down

爱⌒轻易说出口 提交于 2019-12-02 20:54:55

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");
Kiquenet
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;
    }
}
DennisUKSW

You can use this:

webBrowser1.Document.GetElementById("term_id").SetAttribute("value",yourText); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!