WebBrowser control HTMLDocument automate selecting option drop-down

后端 未结 7 2064
-上瘾入骨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 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;
        }
    }
    

提交回复
热议问题