Select Dropdown Value in Selenium | In the previous tutorial, we have known that select class in Selenium is used to interact with dropdown and lists that are created with the <select> element.
We have also familiarized select class methods with examples.
Now, in this tutorial, we will learn how to select dropdown value in Selenium WebDriver.
Let’s create an automation test script for a dropdown control using the select class. This test script will perform to select an option in the list.
In this test, we will use the easiest method selectByVisibleText() to choose the option from the drop-down list. This method chooses the option on the basis of visible text. So, let’s see at a glance at the scenario.
Scenario to Automate:
1. Launch the Firefox browser by creating a WebDriver object.
2. Open the web page URL (https://selenium08.blogspot.com/2019/11/dropdown.html).
3. Locate the dropdown element on the web page.
4. Verify that the dropdown list is enabled and visible.
5. Verify that dropdown allows multiple selections or not.
6. Get the total size of the list.
7. Select the option “India” from the list.
8. Check that “India” is selected as an option or not.
9. At last, close the browser.
10. Compile and run the test script.
Let us look at a glance at the following source code to automate the above scenarios.
Program source code 1:
package seleniumProject; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class SelectByVisibleTextExample { public static void main(String[] args) { // Create a driver object of Firefox class by using reference of WebDriver. WebDriver driver = new FirefoxDriver(); // Maximize the browser. driver.manage().window().maximize(); // Declare a variable URL of type String and store the URL. String URL = "https://selenium08.blogspot.com/2019/11/dropdown.html"; // Call get() method to open the web page URL and pass URL as a parameter. driver.get(URL); // Wait for some time to load the whole web page. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate dropdown element on web page by By.xpath. WebElement dropdown = driver.findElement(By.xpath("//select[@name='country']")); // Verify the dropdown is enabled and visible. if(dropdown.isEnabled() && dropdown.isDisplayed()) { System.out.println("Dropdown is enabled and visible"); } else { System.out.println("Dropdown is not visible"); } // Create an object of Select class and pass the dropdown of type WebElement as an argument. Select select = new Select(dropdown); // Verify that dropdown does not allow the multiple selections. if(select.isMultiple()) { System.out.println("Dropdown list accepts multiple choices"); } else { System.out.println("Dropdown list does not accept multiple choices"); } // Call getOptions() method to get all options of list. size() method is used to get size of list. int listSize = select.getOptions().size(); System.out.println("List size: " +listSize); // Select the option "India" by sending visible text. select.selectByVisibleText("India"); // Check that "India" is selected as an option or not. // Use System.out.println method to display a message to indicate whether the option is chosen and verification was successful or not. String getText = select.getFirstSelectedOption().getText(); System.out.println("Option chosen: " +getText); // Close the browser. driver.close(); } }
Output: Dropdown is enabled and visible Dropdown list does not accept multiple choices List size: 248 Option chosen: India
Explanation:
The following important points with respect to the above scenario are as follows:
1. When the statement WebDriver driver = new FirefoxDriver(); is executed then the driver will launch the Firefox browser.
2. isEnabled() method is used to check whether the dropdown is enabled on the web page or not. Whereas, isDisplayed() method is used to check whether the dropdown list is visible on web page or not.
To learn more in detail, click on this link: Selenium isDisplayed, isEnabled, isSelected Methods
3. The object of select class is created to access the methods of select class that will perform operations on the element.
4. isMultiple() method under the select class has been used to verify that dropdown supports multiple selections or not.
5. select.getOptions().size() is used to get the size of list. Since it will return the size of list in integer, we have stored it in a variable listSize of type int.
6. selectByVisibleText() method will choose the option “India” from the list.
7. The getFirstSelectedOption() of select class will return the option as an instance of WebElement. The getText() method of WebElement class has been used to get text of options returned by getFirstSelectedOption() method.
How to handle Dropdown in Selenium by Value or Index?
We can also choose an option by using its index or value attribute. For example, the following option has value attribute “su” and text label “Sunday”.
In HTML coding, we can write it as:
<option value = "su">Sunday</option>
To choose this option by value, we need to call selectByValue() method of the select class by using the object reference variable of select class, as shown in the following line of code:
select.selectByValue("su");
Similarly, another way to choose an option is by using its index. When options in the list are displayed on the web page, they are indexed in the order in which they are defined on the web page.
By specifying an index value in the selectByIndex() method, we can choose an option from the list. The following code is as follows:
select.selectByIndex(2);
Let’s choose the dropdown option by its value attribute in the same scenario. Follow all the above steps as explained in the following source code.
Program source code 2:
package seleniumProject; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class SelectByValueExample { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); String URL = "https://selenium08.blogspot.com/2019/11/dropdown.html"; driver.get(URL); // Wait for some time to load the whole web page. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate dropdown element on web page by By.xpath. WebElement dropdown = driver.findElement(By.xpath("//select[@name='country']")); // Create an object of Select class and pass the dropdown of type WebElement as an argument. Select select = new Select(dropdown); // Select the option "USA" by sending its value attribute. select.selectByValue("US"); System.out.println("Option is successfully selected"); // Close the browser. driver.close(); } }
Output: Option is successfully selected
The same option “USA” in the list can also be selected by using its index like this:
select.selectByIndex(233);
How to select Multiple values in Dropdown using Selenium Webdriver?
In this section, we will deal with the multiple-choice list. We will use is.Multiple() method to verify the multiple options are selected or not in the list.
The isMultiple() method of select class will return true if the dropdown list supports the multi-selection and false if it does not.
Let’s see the source code to verify it.
Program source code 3:
package seleniumProject; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class multiple-choice { public static void main(String[] args) { WebDriver driver; driver = new FirefoxDriver(); driver.manage().window().maximize(); String URL = "https://selenium08.blogspot.com/2019/11/dropdown.html"; driver.get(URL); // Wait for some time to load the whole web page. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate dropdown element on web page by By.xpath. WebElement dropdown = driver.findElement(By.xpath("//select[@name='Month']")); // Create an object of Select class and pass the dropdown of type WebElement as an argument. Select multiplechoicelist = new Select(dropdown); // Verify that dropdown allows the multiple-choice list or not. if(multiplechoicelist.isMultiple()) { System.out.println( "Dropdown allows multiple-choice list"); } else { System.out.println("Dropdown does not allow multiple-choice"); } // Now select the different choice by using visible text. multiplechoicelist.selectByVisibleText("July"); multiplechoicelist.selectByVisibleText("May"); multiplechoicelist.selectByVisibleText("March"); // Verify that the number of choices in the list selected. if(multiplechoicelist.getAllSelectedOptions().size() == 3) { System.out.println("3 options have been chosen"); } else { System.out.println("Code not worked"); } driver.close(); } }
Output: Dropdown allows multiple-choice list 3 options have been chosen
How to get Selected values from Dropdown in Selenium?
In this section, we will learn how to get all the selected values from the multiple-choice list. To get selected values from the multiple-choice list, we will use a method getAllSelectedOptions() of select class.
The getAllSelectedOptions() method will return all selected options as a list of WebElement.
In this test, we will create a list of selected expected items by using add() method of the List interface, and then selected options returned by the getAllSelectedOptions() will be retrieved in a list by iterating WebElement.
Now let us see the following source code related to it. Follow all the above steps and further steps in the following source code.
Program source code 4:
package seleniumProject; import java.util.ArrayList; 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.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class GetAllSelectedOptions { public static void main(String[] args) { WebDriver driver; driver = new FirefoxDriver(); driver.manage().window().maximize(); String URL = "https://selenium08.blogspot.com/2019/11/dropdown.html"; driver.get(URL); // Wait for some time to load the whole web page. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate dropdown element on web page by By.xpath. WebElement dropdown = driver.findElement(By.xpath("//select[@name='Month']")); // Create an object of Select class and pass the dropdown of type WebElement as an argument. Select multiplechoicelist = new Select(dropdown); // Verify that dropdown allows the multiple-choice list or not. if(multiplechoicelist.isMultiple()) { System.out.println( "Dropdown allows multiple-choice list"); } else { System.out.println("Dropdown does not allow multiple-choice"); } // If multiple-choice allow, select the different choice by sending visible texts. multiplechoicelist.selectByVisibleText("July"); multiplechoicelist.selectByVisibleText("May"); multiplechoicelist.selectByVisibleText("March"); // Verify that the number of choices in the list selected. if(multiplechoicelist.getAllSelectedOptions().size() == 3) { System.out.println("3 options have been chosen:"); } else { System.out.println("Code not worked"); } // Create a List. For this, create an object of ArrayList class by using the reference of List interface. List<String> expectedSelection = new ArrayList<String>(); // Call add() method of List to add expected elements for selection. expectedSelection.add("July"); expectedSelection.add("May"); expectedSelection.add("March"); // Iterating WebElement by using the advanced for loop to retrieve the actually selected elements and then add selected elements in the list using add() method. List<String> actualSelection = new ArrayList<String>(); for(WebElement element : multiplechoicelist.getAllSelectedOptions()) { actualSelection.add(element.getText()); // Here, getText() method of WebElement class has been used to add the text label of all the options in the list. } // Now compare actualSelection with expectedSelection by using containsAll() method to check that correct options are selected in the list.. if(actualSelection.containsAll(expectedSelection)) { System.out.println(actualSelection); } driver.close(); } }
Output: Dropdown allows multiple-choice list 3 options have been chosen: [March, May, July]
How to get all Dropdown values in Selenium WebDriver?
In the earlier section, we saw how to get selected values from the multiple-choice dropdown list in Selenium. By using getOptions() method of the select class, we can also get all the options available in the dropdown list.
Let us see the following source code.
Program source code 5:
package seleniumProject; import java.util.ArrayList; import java.util.Iterator; 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.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class GetAllDropdownValue { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); String URL = "https://selenium08.blogspot.com/2019/11/dropdown.html"; driver.get(URL); // Wait for some time to load the whole web page. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate dropdown element on web page by By.xpath. WebElement dropdown = driver.findElement(By.xpath("//select[@name='Month']")); // Create an object of Select class and pass the dropdown of type WebElement as an argument. Select select = new Select(dropdown); // Call getOptions() method of select class to get all options available in the list. // Since return type of getOptions method is a list of WebElements, therefore, we will store them by creating a variable getOptions of type List of WebElement. List<WebElement> getOptions = select.getOptions(); // Create a list of generic type String to add all dropdown options in the list. List<String> list = new ArrayList<String>(); // Use advanced for loop to add all options in the list. for(WebElement element : getOptions) { list.add(element.getText()); // Here, getText() method of WebElement class has been used to add the text label of all the options in the list. } System.out.println(list); // We can also iterate list one by one by using iterator() method. // So, to iterate list, call iterator() method using reference variable list. Since the return type of iterator method is Iterator of generic type String, therefore, we will store it by using a variable itr of generic type String. // At the beginning, itr (cursor) will point to index just before the first element in the list. Iterator<String> itr = list.iterator(); // Checking the next element availability using reference variable it. while(itr.hasNext()) { // Moving cursor to next element using reference variable it. String str = itr.next(); System.out.println(str); } driver.close(); } }
Output: [Month..., January, February, March, April, May, June, July, August, September, October, November, December] Month... January February March April May June July August September October November December
Hope that this tutorial has covered almost all the important points related to the topic of how to select dropdown value in Selenium WebDriver.
I hope that you will have understood how to select options by using all three methods of select class such as selectByVisibleText(), selectByValue(), and selectByIndex() and performed them.
Thanks for reading!!!Next ⇒ How to deselect dropdown value in Selenium WebDriver⇐ PrevNext ⇒