Understanding Selenium WebDriver API Commands (Part 3)

Operations using Select class in Selenium

Selenium Select class can be found under org.openqa.selenium.support.ui.Select package. It will work for elements with only <select> tag. Some of the operations that can be performed using Select class are discussed below.

Consider the image below with select drop-down having budget options:

Operations using Select class in Selenium

Using this function, you can select all options that display the text matching the given parameter. Here, the parameter text is the visible text to match against. If no matching options are found, then it will throw NoSuchElementException. For example, if you want to select the text ‘Budget’ as shown in the above image, then you can write it as:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’Budget’]”)));

s.selectByVisibleText(“Budget”);

Using this function, you can select the option at the given index. If no matching elements are found, then NoSuchElementException will be thrown. For the first option, the index will start from Zero, so the value at sixth position will be at index 4. For example, if you want to select the option ‘$50000-$100000’ as shown in the above image, then you can write it as:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’Budget’]”)));

s.selectByIndex(5);

Using this function, you can select the option corresponding to the given value. If no matching elements are found, then NoSuchElementException will be thrown. If you want to select the option corresponding to any value given in the image above, you can do it as follows:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’Budget’]”)));

s.selectByValue(“Budget”); 

One thing to note here is that value and the text for that particular option may not be same for every Select element. Also, in some cases the value attribute is not given for Select element, for such elements we can’t use this function.

Using this function, you can get all options that belong to the <select> tag. Here List<WebElement> will be returned. Suppose you want to get the count of total elements within <select> tag and then print the text for the same, this can be done as follows:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’Budget’]”)));

List <WebElement> scount = s.getOptions();

int ssize = scount.size();

 for(int i =0; i>ssize ; i++)

{String Value = scount.get(i).getText();

System.out.println(Value);}

Now, consider the following image referring which we will discuss some multi-select operations:

image002

Using this function, you can check if the select element support selecting multiple options. Accordingly Boolean(true/false) will be returned. Usage example below:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’cars’]”)));

Boolean status = s. isMultiple ();

Using this function, you can get all selected options that belong to the <select> tag. Here List<WebElement> will be returned. You can use this function as follows:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’cars’]”)));

List <WebElement> scount = s. getAllSelectedOptions ();

int ssize = scount.size();

This function is used to return the first selected or currently selected option in the <select> tag. If no option is selected, then it will throw NoSuchElementException. You can use this function as follows:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’cars’]”)));

String text = s. getFirstSelectedOption ().getText();

System.out.println(text);

This function is used to clear all selected entries. This will be valid only when the SELECT supports multiple selections. UnsupportedOperationException will be thrown if the SELECT does not support multiple selections. Usage example below:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’cars’]”)));

s. deselectAll ();

Using this function, we can deselect all options that display text matching the given parameter. If no matching options are found, then it will throw NoSuchElementException. Usage example below:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’cars’]”)));

s.deselectByVisibleText(“Volvo”);

Using this function, we can deselect the option at the given index. If no matching elements are found, then NoSuchElementException will be thrown. Usage example below:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’cars’]”)));

s.deselectByIndex(1);

Using this function, we can deselect the option corresponding to the given value. If no matching elements are found, then NoSuchElementException will be thrown. Usage example below:

Select s = new Select(driver.findElement(By.xpath(“//*[@name=’cars’]”)));

s.deselectByValue(“saab”);

 Also read: Wait Commands in WebDriver to know about different wait commands used in WebDriver.

Also Read: Understanding Selenium WebDriver API Commands (Part 1) & Understanding Selenium WebDriver API Commands (Part 2)

SHARE THIS

Get A Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.