Interacting with Dropdown lists with Selenium in C#

This article briefly explains how to, or at lease how I interact with dropdown menus in C#. Generally speaking I try to pass through the identifier and the value to select to a helper method which carries out the task.

Lets have a look at a typical step definition, first we’re waiting for the element to appear on screen and then selecting the option by value.

[When(@"I select the organisation (.*)'")]
public void WhenISelectTheOrganisation(string orgName)
Page.SelectFromDropDownByClass(ClassName.Organisation, orgName);

So lets look at SelectFromDropDownByClass:

public void SelectFromDropDownByClass(string dropDownClass, string dropdownValue)
new SelectElement(Context.Driver.FindElement(By.ClassName(dropDownClass))).SelectByText(dropdownValue);

We’re creating a new instance of Webdrivers’ SelectElement and passing through the class name and text of the item in the dropdown, keeping the identifier variable ensures we can reuse the same method elsewhere in the solution, you can interact and select items in other ways from dropdowns and also use other identifiers as necessary such as Id, CssSeleector and XPath.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.