Programmatically select an item from the drop-down list in the .NET Webbrowser control

Below is the html script I grabbed from the site. I want to select an 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 that the following code does not work at all.

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

Any quick help would be greatly appreciated.

+4
source share
6 answers

Finally, I will find out with one of my friends. This small feature will make everything very simple.

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

Call function

  this.SetComboItem("MySite.condition_s", "First"); 
+4
source

Have you tried this:

 webBrowser1.Document.GetElementById("MySite.condition_s").selectedIndex = 1 
+2
source

Try it.

  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
source

I founded this, if you just call one click at a time, you can find what you want by clicking inside the loop.

 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"); } } 
0
source

100% working code (tested on win7 - ie11)

taken from:

C # | WebBrowser control - programmatically select an element in 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... } } 
0
source

All Articles