DropDown Handle using selenium:
- In Web page Dropdown / ListBox consider as a “Select” object in WebDriver.
- We declare the Dropdown WebElement as an instance / object of the “Select” class to select any option from DropDown.
- Using “select” method we select and deselect options from DropDown / ListBox.
- “Select” is a class which is provided by Selenium WebDriver to perform multiple operations on DropDown WebElement object and Multiple Select object in Web Page.
- In Selenium select / deselect methods will throw NoSuchElementException if no matching option elements are found in Web Page.
Import below Package to Handle DropDown:
1 | import org.openqa.selenium.support.ui.Select; |
Dropdown Html Code:
1 2 3 4 5 6 7 8 9 | <html> <body> <select id = "Language"> <option value = "val1">Java</option> <option value = "val2">C++</option> <option value = "val3">Python</option> </select> <body> </html> |
Code to Select Dropdown:
1) Identify dropdown Without WebElement:
1 | Select dropdown = new Select(driver.findElement(By.id("Language "))); |
OR
2) Identify dropdown Using WebElement:
1 2 3 | WebElement Lang_Dropdown = driver.findElement(By.id("Language ")); Select dropdown = new Select(Lang_Dropdown); |
Different Dropdown option Select Methods:
- selectByVisibleText Method.
- selectByIndex Method.
- selectByValue Method.
1. selectByVisibleText Method
- It takes a parameter of the string which is a value of the select element and perform select operation.
1 2 3 | Select dropdown = new Select(driver.findElement(By.id("Language "))); dropdown.selectByVisibleText("Java"); |
2. selectByIndex Method
- It fetch a parameter of int which is the index value of Select element and perform select operation.
1 2 3 | Select dropdown = new Select(driver.findElement(By.id("Language "))); dropdown.selectByIndex(1); |
3. selectByValue Method
- It takes a element with string using value and perform select operation.
1 2 3 | Select dropdown = new Select(driver.findElement(By.id("Language "))); dropdown.selectByValue("Value1"); |
Different Dropdown option De-Select Methods:
- deselectByVisibleText Method.
- deselectByValue Method.
- deselectByIndex Method.
- deselectAll Method.
- isMultiple Method.
1. Deselect By Visible Text:
When you wants to remove selection from list box for particular option then you can use webdriver’s deselectByVisibleText() method.
1 2 3 | Select dropdown = new Select(driver.findElement(By.id("Language "))); dropdown.deselectByText("Test"); |
2. Deselect By Value:
You can also deselect option from list box by using value of List Box.
1 2 3 | Select dropdown = new Select(driver.findElement(By.id("Language "))); dropdown.deselectByValue("Test"); |
3. Deselect By Index:
Given bellow syntax will remove selection by index = 1 index available in Integer format.
1 2 3 | Select dropdown = new Select(driver.findElement(By.id("Language "))); dropdown.deselectByIndex(1); |
4. DeselectAll:
Using this method able to Deselect all Selected options from Multi Select Dropdown all options will be UnSelected.
1 2 3 | Select dropdown = new Select(driver.findElement(By.id("Language "))); dropdown.deselectAll(); |
5. isMultiple:
Methods check element support multiple selecting options at the same time or not verify Functionality can we check / select multiple option’s.
1 2 3 | Select dropdown = new Select(driver.findElement(By.id("Language "))); dropdown.isMultiple(); |
Example of Dropdown with Select / De-Select / isMultiple Method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.By; public class DropDown_Handle { public static void main(String[] args) { System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Identify Dropdown Without WebElement Select Country = new Select(driver.findElement(By.name("country"))); // Select Country by selectByVisibleText Country.selectByVisibleText("India"); // Select Country by selectByIndex Country.selectByIndex(1); // Select Country by selectByValue Country.selectByValue("91"); // Deselect Country deselectByVisibleText Country.deselectByVisibleText("India"); // Deselect Country deselectByIndex Country.deselectByIndex(1); // Deselect Country deselectByValue Country.deselectByValue("91"); // Deselect All selected Dropdowns Country.deselectAll(); // Check dropdown have MultiSelect option or not? Country.isMultiple(); } } |
Example of Select Multiple options from Multi Select Dropdown handle using Selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.By; public class Multi_Select_DropDown { public static void main(String[] args) { System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http:www.stqatools.com"); Select hobby = new Select(driver.findElement(By.id("hobby"))); hobby.selectByVisibleText("Cricket"); hobby.selectByIndex(1); hobby.selectByValue("TT"); } } |
Example of Get All Months from dropdown & print:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import java.util.List; import org.openqa.selenium.By; public class Print_All_DropDown_Options { public static void main(String[] args) { System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http:www.stqatools.com"); WebElement month_dropdown = driver.findElement(By.id("month")); Select month = new Select(month_dropdown); List<WebElement> dropdown = month.getOptions(); for (int i = 0; i < dropdown.size(); i++) { String drop_down_values = dropdown.get(i).getText(); System.out.println("dropdown values are " + drop_down_values); } } } |