50% OFF!!!

Thursday, December 22, 2016

c# | WebBrowser control - programmatically select item on html `select`

Hi There,

As many times, we need to interact with the C# (.Net) Webbrowser Control, 
it is useful to be able to interact and programmatically change the selected item 
on an HTML select control.

I don't know, why it is hard to find this easy solution, but the code is as follow:

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... 

And finally, just easy as u code...

If you need change by value and not by index,
I assume you can check the values of the select control (sometimes called: dropdown),
and once you find the correct index of the desired value,
use the code above.


No comments:

Post a Comment