WebBrowser control HTMLDocument automate selecting option drop-down

后端 未结 7 2053
-上瘾入骨i
-上瘾入骨i 2021-02-02 00:28

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 fo

相关标签:
7条回答
  • 2021-02-02 00:55
    HtmlElement hField = webBrowser1.Document.GetElementById("ID");  
    hField.SetAttribute("selectedIndex", "2");  
    

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

    0 讨论(0)
  • 2021-02-02 00:58

    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");
    
    0 讨论(0)
  • 2021-02-02 00:59
    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");
        }
    }
    
    0 讨论(0)
  • 2021-02-02 01:08

    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");
    
    0 讨论(0)
  • 2021-02-02 01:08

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

    0 讨论(0)
  • 2021-02-02 01:13

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题