To perform any action, the first task to do is to identify the element group. Generally, while working with Selenium, you might have to select some values from the drop-down list and also perform other activities and validate them. So, I will guide your way in understanding what is a Select class in Selenium WebDriver and how to Select a value from a drop-down list in Selenium WebDriver. For further details, you can enroll in the Selenium Certification Training.
So, let’s get it started.
Select class in Selenium WebDriver
The Select class is a Webdriver class that basically provides the implementation of the HTML SELECT tag. A Select tag provides the helper methods with select and deselects options. This class can be found under Selenium’s Support.UI.Select package. Select is actually an ordinary class, so its object is also created by the keyword New and also specifies the location of the web element.
Syntax:
Select oSelect = new Select();
It will throw up an error asking to add arguments to the command. So specify the web element location using the element locators.
It clearly states that Select is asking for an element type object for its constructor.
After this, once you get the object of the SELECT Class, you can access all the methods that resides in the SELECT class by typing oSelect + dot which will provide all the methods under the Select class. Choose any method according to your test case.
So, now let’s move ahead to learn about the different methods under this Select class.
Select class in Selenium WebDriver: Different Select commands
Following are the most commonly used methods to deal with the drop-down list.
1. selectByVisibleText: selectByVisibleText(String arg0): void
It is very easy to choose or select an option given under any drop downs and multiple selection boxes with this method. It takes a parameter of String which is one of the value of Select element and it returns nothing.
Syntax: oSelect.selectByVisibleText(“text”);
Example:
Select oSelect =new Select(driver.findElement(By.id("search-box"))); oSelect.selectByVisibleText("Blog");
2. selectByIndex: selectByIndex(int arg0) : void
This method is almost similar to ‘selectByVisibleText’, but the only difference here is that the user has to provide the index number for the option rather the option text. It takes the integer parameter which is the index value of Select element and it returns nothing.
Synatx: oSelect.selectByIndex(int);
Example:
Select oSelect = new Select(driver.findElement(By.id("Seacrch-box"))); oSelect.selectByIndex(2);
3. selectByValue: selectByValue(String arg0) : void
This method again is similar to what I have discussed earlier, the only difference in this method, is that it asks for the value of the option rather the option text or an index. It takes a String parameter which is one of the values of Select element and it does not return anything.
Syntax: oSelect.selectByValue(“text”);
Example:
Select oSelect = new Select(driver.findElement(By.id("Search-box"))); oSelect.selectByValue("Selenium Certification training");
4. getOptions: getOptions( ) : List<WebElement>
This method helps to get all the options belonging to the Select tag. It takes no parameter and returns List<WebElements>.
Syntax: oSelect.getOptions();
Example:
Select oSelect = new Select(driver.findElement(By.id("Search-box"))); List <WebElement> elementCount = oSelect.getOptions(); System.out.println(elementCount.size());
So, let’s move ahead to the next topic and learn about the Multiple Select methods
Select class in Selenium WebDriver: How does Multiple SELECT command work?
The multiple select attribute is a boolean expression. When this is present, it specifies that multiple options can be selected at once. These options vary for different operating systems and browsers namely:
- For Windows: Hold on the control (ctrl) button to select multiple options.
- For Mac: Hold down the command button to select multiple options.
It is user-friendly to use check-boxes instead of using different ways of performing operations because you have to inform the user that multiple selections are available. There is a method which actually helps to specify that you can use multiple select options.
isMultiple
isMultiple(): boolean – This method tells whether the SELECT element supports multiple selection options at the same time or not. This method accepts nothing but returns a boolean value(true/false).
Syntax: oSelect.isMultiple();
Example:
Select oSelect = new Select(driver.findElement(By.id(Element_ID))); oSelect.selectByIndex(index) oSelect.selectByIndex(index) // Or can be used as oSelect.selectByVisibleText(text) oSelect.selectByVisibleText(text) // Or can be used as oSelect.selectByValue(value) oSelect.selectByValue(value)
Select class in Selenium WebDriver: DeSelect methods
When you select a particular element on the webpage, there are a few methods which will help in deselecting that element. But the only challenge in these methods are they do not work for DropDown and only work for Multi-Select elements.
In case you want to deselect any pre-selected option, that can be done with either
- deselectAll()
- deselectByIndex
- deselectByValue
- deselectByVisibletext
Let us understand the methods in detail.
deselectAll(): It clears all selected entries. This is only valid when the drop-down element supports multiple selections.
Example: oSelect.deselectAll();
deselectByIndex(): It deselects the option at the given index.
Example: oSelect.deselectByIndex(2);
- deselectByValue(): This method helps in deselecting the option whose “value” attribute matches the specific parameter.
Example: oSelect.deselectByValue(“13”);
- deselectByVisibletext():This method helps in deselecting the option that displays the text matching the parameter.
Select class in Selenium WebDriver: How to select an option from the drop-down menu?
I will help you guys understand how this Select method works with a real-time example.
In this case, I will consider working on a famous e-commerce website facebook.com.
- First, add the Java libraries onto your system.
- An IDE where you can write the piece of code. I will consider working on the Eclipse IDE as it is user-friendly.
- Add Selenium libraries onto the project.
- Get the URL of the web page.
- Perform desired actions on the drop-down list.
I have explained this using 2 different programs. The first program will help you to select a value from the drop-down list and the second program helps to perform different operations on the drop-down list.
- First, set the browser driver.
- Get the URL of Facebook.
- Create a WebElement object and find the element by using the element locators.
- Select the object of the WebElement using the Select methods.
- Quit the driver execution.
Refer to this code:
package Edurekaa; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; 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 SelectClass { @Test public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "C:UsersVaishnaviDesktopchromedriver_win32 (2)chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.facebook.com"); driver.manage().window().maximize(); //js.executeScript("window.scrollBy(0,300)"); WebElement month_dropdown = driver.findElement(By.id("day")); Select oSelect = new Select(month_dropdown); oSelect.selectByIndex(3); Thread.sleep(3000); WebElement year_yy = driver.findElement(By.id("year")); Select year_y = new Select(year_yy); year_y.selectByValue("2000"); Thread.sleep(3000); WebElement month_m = driver.findElement(By.id("month")); Select month_d1 = new Select(month_m); month_d1.selectByVisibleText("Jul"); driver.quit(); } }
The second program deals with performing actions on the drop-down list. In this case, let us print the number of months and also the names.
- Create a list of WebElements and Select the options.
- Get the size of the month drop-down.
- Print the size of the month list.
- Create another object of the WebElement ele and get the name of the month.
- Print the number using a for loop.
- Quit the driver execution.
package Edurekaa; import java.util.List; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; 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 SelectClass2 { @Test public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "C:UsersVaishnaviDesktopchromedriver_win32 (2)chromedriver.exe"); WebDriver driver = new ChromeDriver(); JavascriptExecutor js = (JavascriptExecutor)driver; driver.get("http://www.facebook.com"); driver.manage().window().maximize(); //js.executeScript("window.scrollBy(0,300)"); WebElement month_dropdown = driver.findElement(By.id("month")); Select oSelect = new Select(month_dropdown); List&amp;lt;WebElement&amp;gt; month_list = oSelect.getOptions(); int total_month = month_list.size(); System.out.println("Total count is "+total_month); for(WebElement ele:month_list) { String month_name = ele.getText(); System.out.println("Months are"+month_name); } driver.quit(); } }
Now with this, we come to an end to this “How to Select from a drop-down in Selenium WebDriver” blog. I Hope you guys enjoyed this article and understood how Select class work in Selenium.
Now that you have understood how to Select a value from a drop-down list using Selenium, check out the Selenium Certification Course by Edureka, a trusted online learning company with a network of more than 650,000 satisfied learners spread across the globe. This course is designed to introduce you to the complete Selenium features and its importance in testing software.
Find out our Selenium Training in Top Cities/Countries
India | Other Cities/Countries |
Bangalore | US |
Hyderabad | UK |
Pune | Canada |
Chennai | Australia |
Mumbai | Singapore |
Kolkata | Edinburgh |
Got a question for us? Please mention it in the comments section of “How to Select from a drop-down in Selenium WebDriver” and we will get back to you.