Select Value from DropDown using Selenium Webdriver: In our previous post, we have discussed how yo can handle checkboxes and radio buttons using the selenium web driver. This post, we are trying to learn rich-featured element Drop down and how can we select multiple values in that.
To perform any operation on the dropdown and multiple select Element, we need to locate that element group because dropdown is not a single element its a group of elements. There is not much difference between these two elements, the only difference is selecting statements, and you can not select more than one value in a dropdown.
Handle DropDown And Multi-Select List Using Selenium WebDriver
To select such elements is like selecting any other elements on a web page. But when it comes to doing any operation on those elements, we need to import a package “org.openqa.selenium.support.ui.Select,” and we need to create an object of the select class.
Select Class in Selenium WebDriver
This Select class provides so many helper methods with the help of those methods we can perform multiple operations on the dropdown and multiple select elements. and you need to pass the element details on which you want to perform operations.
Syntax:
WebElement element = driver.findElement(By.id("testing")); Select oSelect = new Select(element);
Note: Select class only works for those elements who are inside <select> tags.
After creating the object of the select class now, you can get perform different operations with the help of the object. like below:
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class DopdownEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("tools")); Select selectElement=new Select(element); //Select The First Index Element selectElement.selectByIndex(1); Thread.sleep(3000); driver.close(); } }
Select Methods of Select Class
Now we are going to learn different methods by which we can able to select the values from a dropdown or multiple select. Here are some of the methods which we are going to use during selecting the values from a dropdown
- selectByVisibleText()
- selectByIndex()
- selectByValue()
- getOptions()
Let’s try all the above methods one by one with an example:
selectByVisibleText(): This is a straightforward option to select a value from a drop-down and multiple select boxes. This method takes a string argument, which is one of the values of that selected element.
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class selectByVisibleTextEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("tools")); Select selectElement=new Select(element); //Select Selenium Using selectByVisibleText Method selectElement.selectByVisibleText("Selenium"); Thread.sleep(3000); driver.close(); } }
selectByIndex(): This method also works like selectByVisibleText, but the only difference is here we need to provide the index of the required element. Based on the index value, it returns the element.
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class selectByIndexEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("tools")); Select selectElement=new Select(element); //Select Docker Using Index [Index Always Starts From Zero] selectElement.selectByIndex(2); Thread.sleep(3000); driver.close(); } }
selectByValue(): Its also similar to selectByVisibleText and selectByIndex, but the difference is it asks for the value which is described in the DOM that is inside the value attribute like value=”5″. Based on the value, it returns the element.
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class selectByValueEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("tools")); Select selectElement=new Select(element); //Select Cucumber Using Value Attribute selectElement.selectByValue("Cucumber"); Thread.sleep(3000); driver.close(); } }
getOptions(): This method get the all option present inside the Select tag and returns List<WebElements>.
Select oSelect = new Select(driver.findElement(By.id("yy_date_8"))); List <WebElement> elementCount = oSelect.getOptions(); System.out.println(elementCount.size());
package com.selenium.practice.Dropdown; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class getOptionsEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); //Locate The Elements WebElement element=driver.findElement(By.id("tools")); Select selectElement=new Select(element); //Get The Number Of Values Present Inside Dropdown List<WebElement> totalValues=selectElement.getOptions(); System.out.println(totalValues.size() +" Values are Present Inside The Dropdown"); Thread.sleep(3000); driver.close(); } }
Note: You can use all the above methods in both DropDown and Multiple Select Boxes.
isMultiple():
We can use the isMultiple() method to verify that the text box is supported to select multiple values or not. When we execute the method, it will return a boolean value, and based on that, we can find out. If it returns true, that means it selects multiple select, and the false value represents that the box does not support the multiple select.
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class isMultipleEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("multiselectcars")); Select selectElement=new Select(element); //Find Is that Dropdown Supports Multiple Select Or Not //If That Support Multiple Selection Then Returns True else Return False Boolean typeOfDropdown=selectElement.isMultiple(); if(typeOfDropdown) { System.out.println("This Dropdown Supports Multiple Selection"); } else System.out.println("This Dropdown Does Not Support Multiple Selection"); Thread.sleep(3000); driver.close(); } }
Different Deselect methods
The Way we have selected the values by using the different methods, in the same way, we can also deselect the selected values. The only difference is the above-selected methods are work for both dropdown and multiple select boxes, but the deselect methods only work for multiple boxes.
Here the available methods:
- deselectAll(): It Helps you to deselect all select the values.
- deselectByIndex(): It will deselect the given index value only.
- deselectByValue(): It will deselect the value of matching argument.
- deselectByVisibleText(): Deselect all the value what display the matching text.
deselectAll() Example:
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class deselectAllEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("multiselectcars")); Select selectElement=new Select(element); //Deselect All The Selected Values selectElement.selectByIndex(2); Thread.sleep(3000); selectElement.deselectAll(); Thread.sleep(3000); driver.close(); } }
deselectByIndex Example:
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class deselectByIndexEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("multiselectcars")); Select selectElement=new Select(element); //Select Docker Using Index [Index Always Starts From Zero] selectElement.selectByIndex(1); Thread.sleep(3000); selectElement.deselectByIndex(1); driver.close(); } }
deselectByValue Example:
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class deselectByValueEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("multiselectcars")); Select selectElement=new Select(element); //Deselect Audi Using deselectByValue Value Attribute selectElement.selectByValue("audi"); Thread.sleep(3000); selectElement.deselectByValue("audi"); Thread.sleep(3000); driver.close(); } }
deselectByVisibleText Example:
package com.selenium.practice.Dropdown; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class deselectByVisibleTextEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement element=driver.findElement(By.id("multiselectcars")); Select selectElement=new Select(element); //Desklect Audi Using deselectByVisibleText selectElement.selectByVisibleText("Audi"); Thread.sleep(3000); selectElement.deselectByVisibleText("Audi"); Thread.sleep(3000); driver.close(); } }
Note:
If you are try to deselect a dropdown which does not support multiple selection, in that case you will get UnsupportedOperationException with below message in your Editor console.
Exception in thread “main” java.lang.UnsupportedOperationException: You may only deselect options of a multi-select.
If you are trying to locate the element using selectByValue() or deselectByValue(), But if you give wrong value then you will get java.net.SocketTimeoutException
Leave a Reply