views:

884

answers:

2

Hi there.

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.

+1  A: 

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

Dave Hunt
+1 for details.
Jonas Söderström
+1  A: 

I would use storeSelectedValue or getSelectedValue

JUNIT

String value = selenium.getSelectedValue(selectLocator)


Selenium Action

storeSelectedValue ( selectLocator, variableName ) 
Jonas Söderström