从.NET Webbrowser控件的下拉菜单中以编程方式选择一个项目

Programmatically select an item from a drop down in .NET Webbrowser Control
2021-09-23
  •  译文(汉语)
  •  原文(英语)

下面是一个HTML脚本,我是从一个网站上获取的.我想使用.NET以编程方式选择该项目

<div id="MySite.condition_s-wrp" class="c-wrp-slctbx" style="z-index: 1;">
    <input id="MySite.condition_s-input" type="text" autocomplete="off" readonly="readonly" tabindex="0" class=" c-slctbx-medium" style="width: 268px;">
    <ul class="c-ul-slctbx max_height_300" style="width: 285px; display: none; top: 21px;">
        <li id="MySite.condition_s-option-" class="c-li-slctbx">Please choose</li>
        <li id="MySite.condition_s-option-First" class="c-li-slctbx">First</li>
        <li id="MySite.condition_s-option-Second" class="c-li-slctbx">Second</li>
    </ul>
    <select id="MySite.condition_s" name="attributeMap[MySite.condition_s]" class=" c-slctbx-medium" style="display: none;">
        <option value="">Please choose</option>
        <option value="First">First</option>
        <option value="Second">Second</option>
        </select>
</div>

请注意,以下代码根本不起作用.

webBrowser1.Document.GetElementById("MySite.condition_s").SetAttribute("value", "First");

任何快速帮助将不胜感激.

速聊1:
您可以获取selects的子代,并selected在其中一个子代上设置属性.
速聊2:
解释"根本不起作用".
解决过程1

最后,我和一位朋友一起弄清楚了.这个小功能将非常轻松地完成其余的工作.

感谢Farrukh Momin和他的时间.

    public void SetComboItem(string id, string value) {
        HtmlElement ee = this.webBrowser1.Document.GetElementById(id);
        foreach (HtmlElement item in ee.Children) {
            if (item.OuterHtml.ToLower().IndexOf(value.ToLower()) >= 0) {
                item.SetAttribute("selected", "selected");
                item.InvokeMember("onChange");
            }
            else {
                item.SetAttribute("selected", "");
            }
        }

        ee = this.webBrowser1.Document.GetElementById(id + "-input");
        ee.InnerText = value;
    }

通话功能

    this.SetComboItem("MySite.condition_s", "First");
解决过程2

您是否尝试过:

webBrowser1.Document.GetElementById("MySite.condition_s").selectedIndex = 1
速聊1:
恐怕,但是没有selectedIndex属性.
速聊2:
您可能必须先转换为下拉控件.GetElementById返回Element不具有该属性的.
速聊3:
没有一种解决方案有效,我认为与INPUT和UL有关,与SELECT&OPTION有关.我真的在html&javascript :(中差很多.
速聊4:
会不会webBrowser1.Document.GetElementById("MySite.condition_s")返回任何东西?
解决过程3

试试这个.

   HtmlDocument document = webBrowser1.Document;
        HtmlElement siteCondition = document.GetElementById("MySite.condition_s");

        var option = siteCondition.Children.Cast<HtmlElement>().First(x => x.GetAttribute("value").Equals("First"));
        option.SetAttribute("selected", "selected");
速聊1:
没有一种解决方案有效,我认为与INPUT和UL有关,与SELECT&OPTION有关.我真的在html&javascript :(中差很多.
解决过程4

这是您的解决方案,仅通过示例即可:http : //www.vbforums.com/showthread.php?701093-Webbrowser-Control-Select-Dropdownlists-option

或者

http://social.msdn.microsoft.com/Forums/zh-CN/Vsexpressvb/thread/b1273d78-d4af-49e0-9238-6f86e9952484/

速聊1:
没有一种解决方案有效,我认为与INPUT和UL有关,与SELECT&OPTION有关.我真的在html&javascript :(中差很多.
速聊2:
再次冷静地看一下您的代码.您会明白的.所有这些都是最好的.
解决过程5

我发现,如果您只是一个接一个地单击click,那么应该可以通过在其中进行for循环单击来找到所需的内容.

HtmlElement site = this.webBrowser2.Document.GetElementById("myId");
foreach (HtmlElement item in site.Children)
{ 
   if (item.InnerText.ToString() == "something")
   {
       item.InvokeMember("Click");
             break;
   }
       else
   {
       item.InvokeMember("Click");
   }
}
解决过程6

100%工作代码(在win7-ie11上测试)

取自:

C#| WebBrowser控件-以编程方式选择html上的项目选择
http://mdb-blog.blogspot.com/2016/12/c-browser-control-programmatically.html

HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("select") 
foreach (HtmlElement heItem in col) 
{ 
  if (heItem.GetAttribute("className").Contains("exampleClassName") == true) 
  { 
    heItem.SetAttribute("selectedIndex", "3"); // select value at #3
    break; // incase of needed... 
  } 
} 

Below is a html script, I grabbed from a website. I wanna select the item programmatically using .NET

<div id="MySite.condition_s-wrp" class="c-wrp-slctbx" style="z-index: 1;">
    <input id="MySite.condition_s-input" type="text" autocomplete="off" readonly="readonly" tabindex="0" class=" c-slctbx-medium" style="width: 268px;">
    <ul class="c-ul-slctbx max_height_300" style="width: 285px; display: none; top: 21px;">
        <li id="MySite.condition_s-option-" class="c-li-slctbx">Please choose</li>
        <li id="MySite.condition_s-option-First" class="c-li-slctbx">First</li>
        <li id="MySite.condition_s-option-Second" class="c-li-slctbx">Second</li>
    </ul>
    <select id="MySite.condition_s" name="attributeMap[MySite.condition_s]" class=" c-slctbx-medium" style="display: none;">
        <option value="">Please choose</option>
        <option value="First">First</option>
        <option value="Second">Second</option>
        </select>
</div>

Please note the following code is not working at all.

webBrowser1.Document.GetElementById("MySite.condition_s").SetAttribute("value", "First");

Any quick help will be highly appreciated.

Talk1:
You could fetch the selects children and set the attribute selected on one of them.
Talk2:
Explain "is not working at all".
Solutions1

Finally I figure it out with one of my friends. This little function will do the rest very easily.

Thanks to Farrukh Momin and his time.

    public void SetComboItem(string id, string value) {
        HtmlElement ee = this.webBrowser1.Document.GetElementById(id);
        foreach (HtmlElement item in ee.Children) {
            if (item.OuterHtml.ToLower().IndexOf(value.ToLower()) >= 0) {
                item.SetAttribute("selected", "selected");
                item.InvokeMember("onChange");
            }
            else {
                item.SetAttribute("selected", "");
            }
        }

        ee = this.webBrowser1.Document.GetElementById(id + "-input");
        ee.InnerText = value;
    }

Calling Function

    this.SetComboItem("MySite.condition_s", "First");
Solutions2

Have you tried this:

webBrowser1.Document.GetElementById("MySite.condition_s").selectedIndex = 1
Talk1:
I am afraid, but there is no selectedIndex property.
Talk2:
You probably will have to cast to the dropdown control first. GetElementById returns an Element, which doesn't have that property.
Talk3:
None of the solutions are working, I think something to do with the INPUT and UL and not with the SELECT & OPTION. I am really vary bad in html & javascript :(.
Talk4:
Does webBrowser1.Document.GetElementById("MySite.condition_s") return anything at all ?
Solutions3

Try this.

   HtmlDocument document = webBrowser1.Document;
        HtmlElement siteCondition = document.GetElementById("MySite.condition_s");

        var option = siteCondition.Children.Cast<HtmlElement>().First(x => x.GetAttribute("value").Equals("First"));
        option.SetAttribute("selected", "selected");
Talk1:
None of the solutions are working, I think something to do with the INPUT and UL and not with the SELECT & OPTION. I am really vary bad in html & javascript :(.
Solutions4

here is your solution just go through example: http://www.vbforums.com/showthread.php?701093-Webbrowser-Control-Select-Dropdownlists-option

or

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/b1273d78-d4af-49e0-9238-6f86e9952484/

Talk1:
None of the solutions are working, I think something to do with the INPUT and UL and not with the SELECT & OPTION. I am really vary bad in html & javascript :(.
Talk2:
look at your code once again with cool mind.u wil get it.all d best.
Solutions5

I founded that if you just invoke click one by one, you should be able to find what you want by doing a for loop click inside it.

HtmlElement site = this.webBrowser2.Document.GetElementById("myId");
foreach (HtmlElement item in site.Children)
{ 
   if (item.InnerText.ToString() == "something")
   {
       item.InvokeMember("Click");
             break;
   }
       else
   {
       item.InvokeMember("Click");
   }
}
Solutions6

100% working code (tested on win7 - ie11)

taken from:

c# | WebBrowser control - programmatically select item on html select
http://mdb-blog.blogspot.com/2016/12/c-browser-control-programmatically.html

HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("select") 
foreach (HtmlElement heItem in col) 
{ 
  if (heItem.GetAttribute("className").Contains("exampleClassName") == true) 
  { 
    heItem.SetAttribute("selectedIndex", "3"); // select value at #3
    break; // incase of needed... 
  } 
} 
转载于:https://stackoverflow.com/questions/15265539/programmatically-select-an-item-from-a-drop-down-in-net-webbrowser-control

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入