DotNetBrowser DOM API allows working with HTML SELECT and OPTION elements. To work with SELECT element, the DOMElementclass is used. Let's see how to access SELECT element on the web page that contains the following HTML code:


<select id='select-tag'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>


To access SELECT element use the following DotNetBrowser API:


DOMDocument document = browser.GetDocument();
DOMElement select = (DOMElement) document.GetElementById("select-tag");


Now, using the select instance you can get information about its options, programmatically select the specified option. For example:


List<DOMNode> children = selectElement.Children;
List<DOMElement> options = new List<DOMElement>();
foreach (DOMNode child in children)
{
  if (child is DOMElement)
  {
    options.Add((DOMElement)child);
  }
}
DOMElement option = options[index];
option.SetAttribute("selected", "selected");


The complete sample you can find by the following link:


Note: The onchange event is not fired when the dropdown selection is changed programmatically.
 You need to fire the onchange event manually after changing the dropdown selection.

The sample code below demonstrates how to select the option by its value and fire the onchange event:

string expectedValue = "Opel";

List<DOMNode> children = selectElement.Children;
List<DOMElement> options = new List<DOMElement>();
foreach (DOMNode child in children)
{
  if (child is DOMElement)
  {
    DOMElement childElement = child as DOMElement;
    string value = childElement.InnerText;
    Console.WriteLine("Option: "+value);

    if (expectedValue.Equals(value))
    {
      childElement.SetAttribute("selected", "selected");
      var myEvent = e.Browser.CreateEvent("change");
      selectElement.DispatchEvent(myEvent);
    }
  }
}