How to handle Drop Downs using Selenium

Working with drop down options is very easy task when using selenium since we have already drafted method “Select” in selenium. We will use this method to select the desired values from any given drop down. Below is the snippet of code we can use to select value from drop down. If we observe the browser DOM for any drop down field ,we will get Select tag which specifies the drop down. And since we have id for this particular drop down we can proceed as:
Capture
Select select = new Select(driver.findElement(By.id(“dropdown_7”)));
In above code snippet we have created object of Select method which will accept the argument as Webelement.
We can select values from drop down using below mentioned 3 ways:
  1. Select By Index : It will accept integer value and it starts with 0.
  2. Select By Value : It will take value from Value attribute in Select tag.
  3. Select By Visible text : It will take visible text in particular drop down.
Capture
select.selectByIndex(1); — This will select second value from the drop down.
select.selectByValue(“India”); — This will search for India in attribute value and select it.
select.deselectByVisibleText(“India”); — This will Select India value if it is visible in drop down.

Comments

Popular posts from this blog

API Concepts