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

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