【问题标题】:WebBrowser control HTMLDocument automate selecting option drop-downWebBrowser 控件 HTMLDocument 自动选择选项下拉
【发布时间】:2011-01-22 00:47:27
【问题描述】:

我正在尝试使用 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>

对于其他可以通过在文本框中输入值和调用点击事件来学习的人,您可以这样做:

webBrowser1.Document.GetElementById("<HTML ELEMENT NAME>").SetAttribute("value", "THE NAME");

调用按钮或超链接点击:

webBrowser1.Document.GetElementById("<BUTTON>").InvokeMember("click");

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

【问题讨论】:

    标签: c# browser option drop-down-menu


    【解决方案1】:

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

    这应该允许您预先选择第三个值:

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

    【讨论】:

    • 抱歉,这不起作用,因为select 元素没有value 属性。刚刚测试过...
    【解决方案2】:
    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");
        }
    }
    

    【讨论】:

    • 您的建议有效,因为该选项被正确选择(我可以看到它被选中)。但是,与该选项关联的脚本似乎没有运行。当我之后手动选择此选项时,脚本运行良好。你知道为什么吗?如何解决?
    【解决方案3】:

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

    给定:

    <select id="mySelect">
      <option>1</option>
      <option>2</option>
      <option>3</option>
    </select>
    

    以下将选择第三个选项:

    webBrowser1.Document
               .GetElementById("")
               .Children.GetElementsByName("option")[2]
               .SetAttribute("selected", "selected");
    

    【讨论】:

      【解决方案4】:

      试试这个:

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

          Dim cboTemp As mshtml.HTMLSelectElement
          cboTemp = WebBrowser1.Document.GetElementById("myselect").DomElement
          cbotemp.selectedindex = 2
      

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

      【讨论】:

        【解决方案5】:
        HtmlElement hField = webBrowser1.Document.GetElementById("ID");  
        hField.SetAttribute("selectedIndex", "2");  
        

        索引从零开始)而不是......

        【讨论】:

          【解决方案6】:

          五年后,我将在这篇文章上回答,为那些正在寻找这个问题的解决方案的人。

          如果您只需要提交/发布下拉列表的值,那么这一行就足够了:

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

          但是如果你真的需要选择一个底层的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;
              }
          }
          

          【讨论】:

            【解决方案7】:

            你可以用这个:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-01-23
              • 1970-01-01
              • 1970-01-01
              • 2017-12-05
              • 2014-02-02
              • 1970-01-01
              • 2018-06-19
              相关资源
              最近更新 更多