Select Class in Selenium WebDriver: Everything You Need to Know
Select Class in Selenium WebDriver

Select Class in Selenium WebDriver: Everything You Need to Know

Last updated on 07th Jun 2020, Blog, General

About author

Anbarasan (Sr Testing Manager )

He is a TOP-Rated Domain Expert with 11+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 5 Years & Share's this Informative Articles For Freshers

(5.0) | 16534 Ratings 1442

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 you in understanding what is a Select class in Selenium WebDriver and how to Select a value from a drop-down list in Selenium WebDriver.

    Subscribe For Free Demo

    [custom_views_post_title]

    I will be discussing this topic in this order:

    • Select class in Selenium WebDriver
    • Different Select commands
    • How does Multiple SELECT command work?
    • DeSelect Methods
    • How to select an option from the drop-down menu?

    So, let’s get it started.

    Select class in Selenium WebDriver

    The Select class is a Webdriver class which basically provides the implementation of the HTML SELECT tag. A Select tag provides the helper methods with select and deselect 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 reside 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.

    Different Select commands

    Following are the most commonly used methods to deal with the drop-down list.

     1. Select By VisibleText: select By VisibleText(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 values 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 than 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(“Search-box”)));
    • oSelect.selectByIndex(2)
    • 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 than 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”);
    • 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();

    Course Curriculum

    Get Advanced Selenium Training Course for Beginner to Experts

    Weekday / Weekend BatchesSee Batch Details

    Example:

    So, let’s move ahead to the next topic and learn about the Multiple Select methods

    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(): 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:

    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 is 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
    • Deselect By Visible text

    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);

    • Deselect By Value(): This method helps in deselecting the option whose “value” attribute matches the specific parameter.

    Example:

    • oSelect.deselectByValue(“13”);

    • Deselect By Visible text():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:

    • 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.
    Selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download
    • 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;amp;lt;WebElement&amp;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.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free