C# WebBrowser 控件 HTMLDocument 自动选择选项下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2351537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:41:37  来源:igfitidea点击:

WebBrowser control HTMLDocument automate selecting option drop-down

c#browseroptiondrop-down-menu

提问by CWinKY

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:

我正在尝试使用 WebBrowser 控件在 WinForm 中实现自动化,以从网站导航和提取报告信息。您可以在文本框中输入值并调用按钮和链接的单击事件,但我还没有弄清楚如何以自动方式选择选项下拉列表......。任何人都推荐如何从下拉列表中选择一个项目,给定这个 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>

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.

所以我已经解决了输入值和调用点击的问题,但我还没有解决选择下拉值的问题。

采纳答案by Darin Dimitrov

Assuming you have the following select in the HTML:

假设您在 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");

回答by AxelEckenberger

You will have to select the selectedattribute on the option you want.

您必须selected在所需选项上选择属性。

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");

回答by mattb

try this:

尝试这个:

add reference to microsoft.mshtml in project --> add reference...

在项目中添加对 microsoft.mshtml 的引用 --> 添加引用...

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

将变量 cbotemp 设置为 select 元素可以让您更好地访问控件:)

回答by Tom

HtmlElement hField = webBrowser1.Document.GetElementById("ID");  
hField.SetAttribute("selectedIndex", "2");  

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

索引选择(从零开始)而不是....

回答by 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");
    }
}

回答by DennisUKSW

You can use this:

你可以使用这个:

webBrowser1.Document.GetElementById("term_id").SetAttribute("value",yourText); 

回答by TareqNewazShahriar

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:

但是如果你真的需要选择一个底层的 OPTION,那么:

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;
    }
}