DotNetBrowser DOM API allows working with HTML SELECT and OPTION elements. To work with SELECT element, the DOMSelectElementclass 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:

C#

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

VB.NET

Dim document As DOMDocument  = browser.GetDocument()
Dim selectElement As DOMSelectElement = CType(document.GetElementById("select-tag"), DOMSelectElement)


Now, using the select instance you can get information about its options, programmatically select the specified option. To work with OPTION element, the DOMOptionElement class is used. For example:

C#

List<DOMOptionElement> options = selectElement.Options;
DOMOptionElement optionElement = options[index];
optionElement.Selected = true;

VB.NET

Dim options As List(Of DOMOptionElement) = selectElement.Options
Dim optionElement As DOMOptionElement = options.Item(index)
optionElement.Selected = True


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:

C#

string expectedValue = "Opel";

List<DOMOptionElement> options = selectElement.Options;
foreach (DOMOptionElement optionElement in options)
{
    string value = optionElement.InnerText;
    if (expectedValue.Equals(value))
    {
        optionElement.Selected = true;
        var myEvent = e.Browser.CreateEvent("change");
        selectElement.DispatchEvent(myEvent);
    }
}

VB.NET

Dim expectedValue As String = "Opel"

Dim options As List(Of DOMOptionElement) = selectElement.Options
For Each optionElement As DOMOptionElement In options
    Dim value As String = optionElement.InnerText
    If expectedValue.Equals(value) Then
        optionElement.Selected = True
        Dim myEvent = e.Browser.CreateEvent("change")
        selectElement.DispatchEvent(myEvent)
    End If
Next