0% found this document useful (0 votes)
64 views

Dropdown & Multiple Select Operations: Import

This document discusses operations that can be performed on dropdown and multi-select elements in Selenium using the Select class. It provides examples of using methods like selectByIndex, selectByVisibleText, selectByValue, getOptions, deselectAll, and isMultiple. It also includes an example practice exercise to select options from a dropdown on a practice form website.

Uploaded by

Kavitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Dropdown & Multiple Select Operations: Import

This document discusses operations that can be performed on dropdown and multi-select elements in Selenium using the Select class. It provides examples of using methods like selectByIndex, selectByVisibleText, selectByValue, getOptions, deselectAll, and isMultiple. It also includes an example practice exercise to select options from a dropdown on a practice form website.

Uploaded by

Kavitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Just like CheckBox & Radio Buttons, DropDown & Multiple Select Operations also works together and

almost the same way. To perform any action, the first task is to identify the element group. I am saying it
a group, as DropDown /Multiple Select is not a single element. They always have a single name but and
they contain one or more than one element in them. I should rather say more than one option
in DropDown and Multiple Select. The only difference between these two is deselecting statements &
multiple selections are not allowed on DropDown. Let’s look at the different operations:
 

DropDown & Multiple Select Operations


It is just an ordinary operation like selecting any other type of element on a webpage. You can choose it
by ID, Name, CSS & Xpath,  etc. But to perform any action on this element it is required to
import ‘import  org.openqa.selenium.support.ui.Select' package and to use it we need to create a
new Select Object of class Select.
 

Select Class in Selenium


Models a SELECT  tag, providing helper methods to select and deselect options. Select is a class that is
provided by Selenium to perform multiple operations on DropDown object and Multiple Select
objects. This class can be found under the Selenium’s Support.UI.Select package. As Select is also an
ordinary class, so it’s object is also created by a New keyword with regular class creation syntax.
Select oSelect =  new  Select());
The above code will generate compile-time error in Eclipse, as Select() is asking for constructor. Bring
the cursor over Select(), Eclipse will populate a suggestion.

It clearly says that Select is asking for an element type object for its constructor. The code will be:

1 WebElement element = driver.findElement(By.id("Country"));


2 Select oSelect = new Select(element);

4 //Or it can be also written as

6 Select oSelect = new Select(driver.findElement(By.id("Country")));
Note:  Select class only works for elements with <select> tags.
 
Now, once you got the oSelect object which is a SELECT object, you can access all the methods resides
in side the SELECT class by typing oSelect + dot.

Different Select Commands


In this chapter, we will learn how to deal with DropDown and Multi-Select elements. There will be many
interesting operations are available on these elements. But you may be wondering how a DropDown
looks like in the HTML code. We will use the same example for the reference of different select
commands.
 
 

selectByVisibleText
selectByVisibleText(String arg0) : void  – It is very easy to choose or select an option given under any
dropdowns and multiple selection boxes with selectByVisibleText method. It takes a parameter of String
which is one of the Value of  Select element and it returns nothing.
Command – oSelect.selectByVisibleText(“text”);
Example –  Refer the above Screen shot of YEAR Drop Down*
Code –  To select the value 2010.

1 Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));



3 oSelect.selectByVisibleText("2010");
 
 

selectByIndex
selectByIndex(int arg0) : void  – It is almost the same as selectByVisibleText but the only difference here
is that we provide the index number of the option here rather the option text. It takes a parameter of int
which is the index value of Select element and it returns nothing.
Command – oSelect.selectByIndex(int);
Example –  Refer the above Screen shot of YEAR Drop Down*
Code –  To select the value 2010 using index.

1 Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));



3 oSelect.selectByIndex(4);
Note: Index starts from Zero, so the fifth position value  will be at index 4.
 

selectByValue
selectByValue(String arg0) : void  –It is again the same as what we have discussed earlier, the only
difference in this is that it asks for the value of the option rather than the option text or index. It takes a
parameter of String which is on of the value of Select element and it returns nothing.
Command – oSelect.selectByValue(“text”);
Example –  Refer the above Screen shot of YEAR Drop Down*
Code –  To select the value 2014.

1 Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));



3 oSelect.selectByValue("2014");
 Note:  The value of an option and the text of the option may not be always same and there can be a
possibility that the value is not assigned to Select webelement. If the value is given in the Select tag then
only you can use the  selectByValue  method.
 

getOptions
getOptions( ) : List<WebElement>  –This gets the all options belonging to the Select tag. It takes no
parameter and returns List<WebElements>.
Command – oSelect.getOptions();
Sometimes you may like to count the element in the dropdown and multiple select box, so that you can
use the loop on Select element.
Example –  Refer the above Screen shot of YEAR Drop Down*
Code –  To get the Count of the total elements inside SELECT.

1 Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));


2 List <WebElement> elementCount = oSelect.getOptions();
3 System.out.println(elementCount.size());
 

Print all the Options


Code –  To get the Count of the total elements inside SELECT  and to Print the text value of every element
present in the  SELECT.

1 Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));


2 List <WebElement> elementCount = oSelect.getOptions();
3 int iSize = elementCount.size();

5 for(int i =0; i<iSize ; i++){
6 String sValue = elementCount.get(i).getText();
7 System.out.println(sValue);
8 }
All of the above methods work on both Dropdown and Multiple select box.
 

DeSelect Methods
The way we select different values of DropDown & Multi Select, the same way we can also deselect  the
values. 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 and deselectByVisibletext.
 
Multi Select element look like this:

deselectAll( ) :  void  – Clear all selected entries. This is only valid when the SELECT supports multiple
selections.
Command – oSelect.deselectAll;
deselectByIndex(int arg0) :  void  –Deselect the option at the given index.
Command – oSelect.deselectByIndex;
deselectByValue(String arg0) :  void  –Deselect all options that have a value matching the argument.
Command – oSelect.deselectByValue;
deselectByVisibleText(String arg0) :  void  – Deselect all options that display text matching the
argument.
Command – oSelect.deselectByVisibleText
 

isMultiple
isMultiple( ) : boolean  – This tells whether the SELECT element support multiple selecting options at the
same time or not. This accepts nothing but returns boolean value(true/false).
Command – oSelect.isMultiple();
This is done by checking the value of the “multiple” attribute.
Example –  Refer the above Screen shot of MULTI SELECT for  multiple  attribute*
 
 
Multi Select Methods
This one also just works on Multiple selection boxes and not on regular List boxes or dropdowns. There
is no additional logic behind selecting multiple options of Select element. All you need to do is to fire
select commands on multiple elements one by one that’s it.

1 Select oSelect = new Select(driver.findElement(By.id(Element_ID)));


2 oSelect.selectByIndex(index)
3 oSelect.selectByIndex(index)
4  
5 // Or can be used as
6  
7 oSelect.selectByVisibleText(text)
8 oSelect.selectByVisibleText(text)
9  
10 // Or can be used as
11  
12 oSelect.selectByValue(value)
13 oSelect.selectByValue(value)
 

Practice Exercise -1 (Drop Down Box/List)


1. Launch new Browser
2. Open “http://toolsqa.com/automation-practice-form/”
3. Select ‘Continents’ Drop down ( Use Id to identify the element )
4. Select option ‘Europe’ (Use selectByIndex)
5. Select option ‘Africa’ now (Use selectByVisibleText)
6. Print all the options for the selected drop down and select one option of your choice
7. Close the browser
Solution

1 package automationFramework;
2 import java.util.List;
3 import java.util.concurrent.TimeUnit;
4 import org.openqa.selenium.By;
5 import org.openqa.selenium.WebDriver;
6 import org.openqa.selenium.WebElement;
7 import org.openqa.selenium.firefox.FirefoxDriver;
8 import org.openqa.selenium.support.ui.Select;
9 public class DropDownCommands {
10 public static void main(String[] args) throws InterruptedException {
11 // Create a new instance of the FireFox driver
12 WebDriver driver = new FirefoxDriver();
13  
14 // Put an Implicit wait,
15 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
16  
17 // Launch the URL
18 driver.get("http://toolsqa.com/automation-practice-form");
19  
20 // Step 3: Select 'Continents' Drop down ( Use Id to identify the element )
21 // Find Select element of "Single selection" using ID locator.
22 Select oSelect = new Select(driver.findElement(By.id("continents")));
23  
24 // Step 4:) Select option 'Europe' (Use selectByIndex)
25 oSelect.selectByVisibleText("Europe");
26  
27 // Using sleep command so that changes can be noticed
28 Thread.sleep(2000);
29  
30 // Step 5: Select option 'Africa' now (Use selectByVisibleText)
31 oSelect.selectByIndex(2);
32 Thread.sleep(2000);
33  
34 // Step 6: Print all the options for the selected drop down and select one option of your choice
35 // Get the size of the Select element
36 List<WebElement> oSize = oSelect.getOptions();
37 int iListSize = oSize.size();
38  
39 // Setting up the loop to print all the options
40 for(int i =0; i < iListSize ; i++){
41 // Storing the value of the option
42 String sValue = oSelect.getOptions().get(i).getText();
43 // Printing the stored value
44 System.out.println(sValue);
45 // Putting a check on each option that if any of the option is equal to 'Africa" then select it
46 if(sValue.equals("Africa")){
47 oSelect.selectByIndex(i);
48 break;
49 }
50 }     
51 // Kill the browser
52 driver.quit();
53 }
54 }
 

Practice Exercise -2 (Multiple Selection Box/List)


1. Launch new Browser
2. Open “http://toolsqa.com/automation-practice-form/”
3. Select ‘Selenium Commands’ Multiple selection box ( Use Name locator to identify the element )
4. Select option ‘Browser Commands’  and then deselect it (Use selectByIndex and
deselectByIndex)
5. Select option ‘Navigation Commands’  and then deselect it (Use selectByVisibleText and
deselectByVisibleText)
6. Print and select all the options for the selected Multiple selection list.
7. Deselect all options
8. Close the browser
Solution

1 package automationFramework;
2  
3 import java.util.List;
4 import java.util.concurrent.TimeUnit;
5  
6 import org.openqa.selenium.By;
7 import org.openqa.selenium.WebDriver;
8 import org.openqa.selenium.WebElement;
9 import org.openqa.selenium.firefox.FirefoxDriver;
10 import org.openqa.selenium.support.ui.Select;
11  
12 public class MultiSelectCommands {
13  
14 public static void main(String[] args) throws InterruptedException {
15 // Create a new instance of the FireFox driver
16 WebDriver driver = new FirefoxDriver();
17  
18 // Put an Implicit wait
19 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
20  
21 // Launch the URL
22 driver.get("http://toolsqa.com/automation-practice-form");
23  
24 // Step 3: Select 'Selenium Commands' Multiple select box ( Use Name locator to identify the
element )
Select oSelect = new Select(driver.findElement(By.name("selenium_commands")));
25
 
26
// Step 4: Select option 'Browser Commands' and then deselect it (Use selectByIndex and
27
deselectByIndex)
28
oSelect.selectByIndex(0);
29
Thread.sleep(2000);
30
oSelect.deselectByIndex(0);
31
 
32
// Step 5: Select option 'Navigation Commands'  and then deselect it (Use selectByVisibleText and
33
deselectByVisibleText)
34
oSelect.selectByVisibleText("Navigation Commands");
35
Thread.sleep(2000);
36
oSelect.deselectByVisibleText("Navigation Commands");
37
 
38
// Step 6: Print and select all the options for the selected Multiple selection list.
39
List<WebElement> oSize = oSelect.getOptions();
40
int iListSize = oSize.size();
41
 
42
// Setting up the loop to print all the options
43
for(int i =0; i < iListSize ; i++){
44
// Storing the value of the option
45
String sValue = oSelect.getOptions().get(i).getText();
46
 
47
// Printing the stored value
48
System.out.println(sValue);
49
 
50
// Selecting all the elements one by one
51
oSelect.selectByIndex(i);
52
Thread.sleep(2000);
53
}
54
 
55
// Step 7: Deselect all
56
oSelect.deselectAll();
57
 
58
// Kill the browser
59
driver.close();
60
}
}
 

You might also like