Selenium: get current value from dropdown menu

I am trying to find a simple Selenium call to grab the current option from the select dropdown. I know that there are calls that capture all the values ​​in the list, but I want to know which option is selected. Sorry if this is trivial, but google and Selenium IDE didn't help me. Thanks.

+4
source share
3 answers

You should be able to use the getSelected * commands to return the identifier, index, or label of the selected item. The following is a quote from the Selenium link:


storeSelectedId (selectLocator, variableName)
Gets the element element identifier for the selected parameter in the specified selection element.

Arguments:

  • selectLocator - element locator that defines the drop-down menu
  • variableName - the name of the variable in which the result should be stored.

Returns: the selected option identifier from the drop-down list select


storeSelectedIndex (selectLocator, variableName)
Returns the index of options (option number, starting at 0) for the selected parameter in the specified selection.

Arguments:

  • selectLocator - element locator that defines the drop-down menu
  • variableName - the name of the variable in which the result should be stored.

Returns: the selected option index in the drop-down list, select


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

Arguments:

  • selectLocator - element locator that defines the drop-down menu
  • variableName - the name of the variable in which the result should be stored.

Returns: the selected option label in the drop-down list, select

+5
source

I would use storeSelectedValue or getSelectedValue

Junit

 String value = selenium.getSelectedValue(selectLocator) 


Selenium action

 storeSelectedValue ( selectLocator, variableName ) 
+2
source

There is a link for practicing such things:

" https://letskodeit.teachable.com/p/practice "

there "An example of class selection"

1. in this test, he first clicks on “Honda” in the drop-down menu 2. then extracts the selection tag as the parent tag of the Honda tag 3. Then converts it to Select Object 4. Then I use getFirstSelectedOption () to compare it with the expected meaning "Honda.

  @Test public void selectTagDemo() { WebElement hondaElement = webDriver.findElement(By.xpath("//option[@value=\"honda\"]")); hondaElement.click(); WebElement selectCarWebElement = hondaElement.findElement(By.xpath("//parent::select")); Select selectCar = new Select(selectCarWebElement); Assert.assertEquals(selectCar.getFirstSelectedOption().getText(), "Honda"); } 

if you need all test class comment below

0
source

Source: https://habr.com/ru/post/1300024/


All Articles