Getting the selected item from a drop down list

In Selenium IDE you could simply use getSelectedLabel | locator and that was your currently selected item in the drop down list. Its a bit more of a roundabout way with WedDriver as you can see from the method I have written below:

using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;

// ... 

public string getSelectedLabel(ddlDropListID)
   string selected;
   SelectElement selectOption = new SelectElement(ddlDropListID);
   selected = selectOption.SelectedOption.Text;
   return selected;

While there is a few more lines of code required, it is a short enough snippet to learn.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s