How To Handle Dropdown in Selenium Webdriver Using Java

.
Handling Dropdown In Selenium

In this tutorial we are going to learn how to select values from the dropdown,- Static Dropdown, Dynamic Dropdown, and Autosuggestion Dropdown using Selenium and how to automate dropdown in Selenium web driver and multiple select operations. 

How To Handle Static Dropdown in Selenium Webdriver?
 
Select class in selenium 
In selenium, the select class is used to select or deselect values from the dropdown.

How To Select  Value From the Dropdown in Selenium using Java?
The basic Selenium Code to select value From the Dropdown is given below.

Selenium Code:

public class StaticDropdown {


public static void main(String[] args) {

// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver-win64 (1)\\chromedriver-win64\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("https://rahulshettyacademy.com/dropdownsPractise/");

WebElement dd= driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));

//click on Dropdown

dd.click();

Select dropdn =new Select(dd);

//Select the value from dropdown which position is second

dropdn.selectByIndex(1);


}

How To Select the value from the dropdown and Verify that the Selected value is Right or Wrong?

To Verify selected dropdown value is right or wrong check the below selenium Code. 

Selenium Code:

public class StaticDropdown {


public static void main(String[] args) {

// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver-win64 (1)\\chromedriver-win64\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("https://rahulshettyacademy.com/dropdownsPractise/");

WebElement dd= driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));

//click on Dropdown

dd.click();

Select dropdn =new Select(dd);


//Select the value from dropdown which position is second

dropdn.selectByIndex(2);

WebElement en= dropdn.getFirstSelectedOption();

String value =en.getText();

if (value.equals("AED"))

{

System.out.println("Right");

}

else {

System.out.println("Wrong");

}


Selenium web driver provides three ways to select an option from the dropdown

  • selectByIndex() 
  • SelectByValue()
  • selectByVisibleText()

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

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


SelectByValue()
This method is used to select a value from the dropdown based on its "value" attribute.

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

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

Selenium Code
//How to select the 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 Web Driver Java?

In Selenium select class provides multiple methods to get value from the dropdown.
  • getOptions()
  • getFirstSelectedOptions()
  • getAllSelectedOptions()

How To Get and Print All the Values of the Drop-Down?

getOptions()
getOptions is a method of selecting a class that is used to get all options in a dropdown or multi-select 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 the above code we can retrieve and print all the options of a dropdown.

How To Get and Print the First Selected Options from the Dropdown?

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

Selenium Code
//How to get the 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 the above code we can retrieve and print the first selected options of a dropdown.

How To Get and Print All Selected Options of Dropdown?

getAllSelectedOptions()
Using this method we can retrieve all the selected options of the dropdown, whether single-select or multi-select.
 
Selenium Code 
//How to get all selected options 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 the above code we can retrieve and print all selected options of a dropdown.


How To Deselect Value from the Dropdown in the Selenium Web Driver?

Select class in the selenium web driver provides multiple ways to deselect value from the dropdown.

deselectAll()
deselectAll() is a method of selecting classes in the Selenium web driver which is used to deselect all the selected options in the 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 selecting a class in Selenium web driver which is used to deselect a value from the 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 a select class that 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 a method of selecting a class in Selenium that is used to deselect value from the 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 The Dropdown is Multi-select?

isMultiple()
In the selenium web driver to verify whether the dropdown is multi-select or not selenium select class provides is multiple () 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 multi-select 
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
boolean hd=dropd.isMultiple();
System.out.println(hd);

How to Verify that the dropdown is static?

A dropdown is known as static if it starts with a "select" tag.

Example : 



Hope !!! The above Tutorial on How to handle dropdowns in the Selenium web driver is helpful for you...

How To Handle Dynamic Dropdown in Selenium? 
How To Handle Auto Suggestion Dropdown in Selenium? 

Team,
QA acharya

Tags: How to select a value from the dropdown, how to deselect a value from the dropdown in selenium, how to get a selected value from the dropdown in selenium, Select a class in selenium web driver, 


how to handle Dropdown in selenium webdriver



Post a Comment

0 Comments