How to Select a Value from a Dropdown

0
36

We can select a value from a dropdown by using WebDriver’s Select class.

Syntax:

  1. selectByValue:
  2. Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));
  3. selectByValue.selectByValue(“greenvalue”);
  4.  
  5. selectByVisibleText:
  6. Select selectByVisibleText = new Select (driver.findElement(By.id(“SelectID_Two”)));
  7. selectByVisibleText.selectByVisibleText(“Lime”);
  8.  
  9. selectByIndex:
  10. Select selectByIndex = new Select(driver.findElement(By.id(“SelectID_Three”)));
  11. selectByIndex.selectByIndex(2);
  1. public Select(WebElement element)

A check is made that the given element is, indeed, a SELECT tag. If it is not, then an UnexpectedTagNameException is thrown.
Parameters:element – SELECT element to wrapThrows:UnexpectedTagNameException – when element is not a SELECT

Facebook Comments
(Visited 37 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here