How To Handle Dropdown in Selenium Webdriver Using Java

                Handling Dropdown In Selenium

In this tutorial we are going to learn how to select value from dropdown using selenium and how to automate dropdown in selenium webdriver and multiple select operation. 

Select class in selenium 
In selenium select class is used to selecting or deselecting value from dropdown.

1- How to select value from dropdown in selenium using java ?
Selenium web driver provides three way to select an options from dropdown

1-selectByIndex()  - This method is used to select option at given index , beginning with zero.

Selenium Code
//How to select value from dropdown -index
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
    dropd.selectByIndex(4);

2-SelectByValue() - This method is used to select value from dropdown based on it "value" attribute.

Selenium Code
//How to select value from dropdown -value
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
    dropd.selectByValue("Manual");

 3-selectByVisibleText() - This method is used to select option from dropdown based on the text over the option.


Selenium Code
//How to select value from dropdown - Visible Text
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
    dropd.selectByVisibleText("Database Testing");     

How to Get all the options from dropdown using selenium webdriver java ?

In Selenium select class provide multiple method to get value form dropdown .
  • getOptions()
  • getFirstSelectedOptions()
  • getAllSelectedOptions()

  • How to get and print all the value of drop down ?

getOptions()
getOptions is a method of select class which is used to get all option in a dropdown or mutliselect box 

Selenium Code

/How to get all the options from dropdown 
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
List<WebElement> list = dropd.getOptions();
System.out.println("The dropdown options are:");
        for(WebElement options: list)
            System.out.println(options.getText());

Using above code we can retrieve and print all the options of a dropdown.

  • How to get and print first selected options from dropdown?

getFirstSelectedOptions()
Using this method we get the first selected options of dropdown whether dropdown is single select -dropdown or multi select drop down .

Selenium Code

//How to get first Selected option of dropdown
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
 WebElement firstop =dropd.getFirstSelectedOption();
String val=firstop.getText();
 System.out.println(val);

 Using above code we can retrieve and print first selected options of a dropdown.


  • How to get and print all selected option of dropdown ?

getAllSelectedOptions()
Using this method we can retrieve all the selected option of the dropdown whether the dropdown is single select or multi select .
 
Selenium Code 
//How to get all selected option of dropdown
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
List<WebElement> list = dropd.getAllSelectedOptions();
for(WebElement options: list)
            System.out.println(options.getText());

Using above code we can retrieve and print all selected options of a dropdown.


  • How to deselect value from dropdown in selenium webdriver ?

Select class in selenium webdriver provide multiple way to deselect value from dropdown.


  • deselectAll()
deselectAll() is method of select class in selenium webdriver which is used to deselect all the selected option in dropdown.

Selenium Code 
//How to deselect all value from dropdown 
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
    dropd.deselectAll();

  • deselectByIndex()
deselectByIndex() is also a method of select class in selenium webdriver which is used to deselect a value from dropdown by specifying its index.

Selenium Code 
//How to deselect value from dropdown -index
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
    dropd.deselectByIndex(4);

  • deselectByValue()
deselectByValue() method in selenium is a method of select class class which is used to deselect a selected value of dropdown by specifying its value .

Selenium Code 
//How to deselect value from dropdown -value
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
    dropd.deselectByValue("Manual");

  • deselectByVisibleText()
deselectByVisibleText() is method of select class in selenium which is used to deselect value from dropdown by specifying visible text .

Selenium Code 
//How to deselect value from dropdown -visibletext
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
    dropd.deselectByVisibleText("Manual Testing");

  • How to verify /check whether dropdown is multiselect ?
isMultiple()
In selenium webdriver to verify whether the dropdown is multiselect or not selenium select class provide isMultiple () method . 

Selenium Code-1
//To verify whether dropdown is multiselect 
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
if(dropd.isMultiple())
{
System.out.println("YES dropdon is multiselect");
}
else
{
System.out.println("NO dropdown is not multiselect");
}

Selenium Code-2

//To check that dropdown is multiselect 
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
boolean hd=dropd.isMultiple();
System.out.println(hd);

Hope !!! Above tutorial How to handle dropdown in selenium webdriver helpful for you ....

Team,
QA acharya

Tags- How to select value from dropdown , how to deselect value from dropdown in selenium , how to get selected value from dropdown in selenium , Select class in selenium wbedriver , 




how to handle Dropdown in selenium webdriver



Post a Comment

0 Comments