Professional Documents
Culture Documents
Dropdown & Multiple Select Operations: Select Class in Selenium
Dropdown & Multiple Select Operations: Select Class in Selenium
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:
It clearly says that Select is asking for an element type object for its constructor. The code will
be:
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.
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.
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.
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*
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 )
25 Select oSelect = new Select(driver.findElement(By.name("selenium_commands")));
26
27 // Step 4: Select option 'Browser Commands' and then deselect it (Use selectByIndex and 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 deselectByVisibleText)
33 oSelect.selectByVisibleText("Navigation Commands");
34 Thread.sleep(2000);
35 oSelect.deselectByVisibleText("Navigation Commands");
36
37 // Step 6: Print and select all the options for the selected Multiple selection list.
38 List<WebElement> oSize = oSelect.getOptions();
39 int iListSize = oSize.size();
40
41 // Setting up the loop to print all the options
42 for(int i =0; i < iListSize ; i++){
43 // Storing the value of the option
44 String sValue = oSelect.getOptions().get(i).getText();
45
46 // Printing the stored value
47 System.out.println(sValue);
48
49 // Selecting all the elements one by one
50 oSelect.selectByIndex(i);
51 Thread.sleep(2000);
52 }
53
54 // Step 7: Deselect all
55 oSelect.deselectAll();
56
57 // Kill the browser
58 driver.close();
59 }
60 }