Selecting dropdown option with Selenium
In this article, I will discuss dropdown selection with Selenium WebDriver which is an important element of any webpage. There are two types of dropdowns: Single value Dropdown and Multi-Value Dropdown. We can select dropdown options using different techniques based on the situations. Here, I will cover all these techniques. Moreover, there are some cases where the dropdown menu is developed without the HTML select tag, in that situation we cannot use the Select class. So that part I will cover as well.
1. Select a particular value in a dropdown using the methods of Select class in Selenium
Select class implement select tag, providing helper methods to select and deselect options.
WebElement dropdownlist = driver.findElement(By.xpath(locator));
Select listbox = new Select(dropdownlist);
Below is the sample demo application dropdown:
Use of methods of select class with examples:
selectByIndex(int index): Select the option at the given index.
listbox.selectByIndex(2);
Below is the code snippets from eclipse editor:
selectByVisibleText(java.lang.String text): Select all options that display text matching the argument
listbox.selectByVisibleText(“Date”);
Below is the code snippets from eclipse editor:
selectByValue(java.lang.String value): Select all options that have a value matching the argument.
listbox.selectByValue(“1”);
Below is the code snippets from eclipse editor:
getOptions(): Get all options from the dropdown list.
List <WebElement> elementCount = listbox.getOptions();
System.out.println(elementCount.size());
isMultiple(): Verify whether element support selecting multiple options at the same time. This is done by checking the value of the “multiple” attribute. It will return boolean (true or false) value.
boolean value = listbox.isMultiple();
getFirstSelectedOption(): The first selected option in this select tag (or the currently selected option in a normal select)
listbox.getFirstSelectedOption( );
getAllSelectedOptions(): All selected options belonging to this select tag
WebElement element = driver.findElement(By.xpath(locator));
Select select = new Select(element);
List<WebElement> selectedOptions = select.getAllSelectedOptions();
deselectAll(): Clear all selected entries. This is only valid when the SELECT supports multiple selections.
listbox.deselectAll();
deselectByIndex(int index): Deselect the option at the given index.
listbox.deselectByIndex(0);
deselectByValue(java.lang.String value): Deselect all options that have a value matching the argument.
listbox.deselectByValue(“aaa”);
deselectByVisibleText(java.lang.String text): Deselect all options that display text matching the argument.
listbox.deselectByVisibleText(“mango”);
2. Select a particular value in a dropdown without using the methods of Select class in Selenium
Using sendkeys method:
driver.findElement(By.id(“dropdownField”)).sendKeys(“English”); //send required option in dropdown field
driver.findElement(By.id(“option1”)).click(); //click on required value
Using keyboard actions commands:
WebElement products=Driver.findElement(By.xpath(“//input[@id=’react-select-3-input’]”));
products.sendKeys(“Uttar Pradesh”);
products.sendKeys(Keys.ARROW_DOWN);
products.sendKeys(Keys.ENTER);
Below is the code snippets from eclipse editor:
Using findElements method fetch all options:
List<WebElement> options = driver.findElements(By.xpath(“”));
for(WebElement option : options) {
if (option.getText().contains(“FA Productivity”)) {
option.click();
break;
}
}
Using Action class or Mouse-Hover on Option:
WebElement wb1 = webDriver.findElement(By.xpath(“//*[@id=’1692']”));
Actions mouse = new Actions(webDriver);
mouse.moveToElement(wb1).click(); //click the dropdown menu
WebElement wb2 = webDriver.findElement(By.xpath(“//*[@id=’30241']”));
mouse.moveToElement(wb2).click(); //click the required option
mouse.build().perform(); //perform the operation
Using click method:
//To click the dropdown menu
driver.findElement(By.xpath(“.//*[@id=’type’]”)).click();
//To click the required option
driver.findElement(By.xpath(“.//*[@id=’type’]/option[3]”)).click();
Using JavascriptExecutor Interface:
WebElement listbox = driver.findElement(By.xpath(“//button[@id=’custom’]”));
// casting a driver instance into JavascriptExecutor instance
JavascriptExecutor js = (JavascriptExecutor) driver;
// set the dropdown value to ‘mango’ using javascript
js.executeScript(“arguments[0].value=’mango’”, listbox);