Selenium: Get current value from drop-down menu

tombh picture tombh · Feb 2, 2010 · Viewed 20.4k times · Source

I'm trying to find a simple Selenium call to grab the current option from a select drop-down list. I'm aware there are calls which grab all the values in a list but I wish to know which option is currently selected. Apologies if this is trivial but google and Selenium IDE didn't help me. Thanks.

Answer

Dave Hunt picture Dave Hunt · Feb 2, 2010

You should be able to use the getSelected* commands to return the ID, index, or label of the selected item. Below is quoted from the Selenium Reference:


storeSelectedId ( selectLocator, variableName )
Gets option element ID for selected option in the specified select element.

Arguments:

  • selectLocator - an element locator identifying a drop-down menu
  • variableName - the name of a variable in which the result is to be stored.

Returns: the selected option ID in the specified select drop-down


storeSelectedIndex ( selectLocator, variableName )
Gets option index (option number, starting at 0) for selected option in the specified select element.

Arguments:

  • selectLocator - an element locator identifying a drop-down menu
  • variableName - the name of a variable in which the result is to be stored.

Returns: the selected option index in the specified select drop-down


storeSelectedLabel ( selectLocator, variableName )
Gets option label (visible text) for selected option in the specified select element.

Arguments:

  • selectLocator - an element locator identifying a drop-down menu
  • variableName - the name of a variable in which the result is to be stored.

Returns: the selected option label in the specified select drop-down