1.

How to handle a dropdown in Selenium WebDriver? How to select a value from the dropdown?

Answer»

Questions on the dropdown and selecting a value from the dropdown are common Selenium interview questions.

You should know that to work with a dropdown in Selenium, always make use of HTML tag: ‘select’. Without using ‘select’, you cannot handle dropdowns. 

So the STEPS to select an element from the drop-down are -

  1. Identify the ‘select’ HTML tag as dropdowns must have the ‘select’ tag)
  2. Select an option from that dropdown element

To identify the ‘select’ html element from the web page, use findElement() method. Look at the below piece of code:

  • WebElement mySelectElement = driver.findElement(By.id("id value"));
  • Select dropdown = new Select(mySelectElement);

Now to select an option from that dropdown can be done in THREE WAYS:

  • dropdown.selectByVisibleText(“text1”); → Select an option by the visible text.
  • dropdown.selectByIndex(“1”); → Select, by choosing the INDEX number of a option.
  • dropdown.selectByValue(“value”); → Select by choosing the value.

These are the different ways to select a value from a dropdown.



Discussion

No Comment Found