Selenium Course
- 62k Enrolled Learners
- Weekend/Weekday
- Live Class
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.
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.
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
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:
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(): 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)
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
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);
Example: oSelect.deselectByValue(“13”);
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.
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.
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.
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.
Course Name | Date | Details |
---|---|---|
Selenium Course | Class Starts on 23rd November,2024 23rd November SAT&SUN (Weekend Batch) | View Details |
Selenium Course | Class Starts on 25th November,2024 25th November MON-FRI (Weekday Batch) | View Details |
Selenium Course | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
edureka.co