
Working with Dropdown
Dropdown is a kind of WebElement which consists of multiple options. HTML tag “select” is used to define a dropdown. HTML structure of a dropdown in Wikipedia page is displayed as:
In above figure, select tag has an id “searchLanguage” which alone help in identifying this locator uniquely. Upon expanding the select tag you’ll notice another tag ‘option’. This option tag has parameters like value and actual text which is displayed on the website.
Select select = new Select(driver.findElement(By.id("searchLanguage")));
Above statement will assign the reference of a WebElement with id as “searchLanguage” to a variable select. Now using this select we may perform following:
select.getAllSelectedOptions(); //Return a List(WebElement type) of default selected value(s). select.getOptions(); // This will return a List of all the WebElements stored in the dropdown. select.selectByIndex(2); // This will select the third value from the dropdown. select.selectByValue("cs"); // This will set the text mapped with value “cs”.
In your existing project, add these two methods:
public void getDropdownValue() { Select select = new Select(driver.findElement(By.id("searchLanguage"))); List<WebElement> options = select.getOptions(); for (WebElement option : options) { System.out.println(option.getText()); } } public void indexAndValue() { Select select = new Select(driver.findElement(By.id("searchLanguage"))); select.selectByIndex(2); select.selectByValue("cs"); } }
Add following code to your main method:
obj.getDropdownValue(); obj.indexAndValue();
When you run this, obj.getDropdownValue() will be executed and it will print all the values in dropdown on console. Next, obj.indexAndValue() will be executed and it will first select the value of text at third position and then it will select the text which has value “cs”.
There are many other Select methods which are very helpful and extensively used in real time, I encourage you to go through all the Select methods and use them on any test website.
The whole code of what we have done so far is below:
package ch01_WebDriverInterface; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.Select; public class DropdownValues { public WebDriver driver; public static void main(String[] args) { DropdownValues obj = new DropdownValues(); obj.getUrl(); // obj.enterText(); obj.getDropdownValue(); obj.indexAndValue(); } public void getUrl() { System.setProperty("webdriver.chrome.driver", "/home/dj/eclipse-workspace/SeleniumAutomation/Driver/chromedriver"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); // open Browser in maximized mode options.addArguments("disable-infobars"); // disabling infobars options.addArguments("--disable-extensions"); // disabling extensions options.addArguments("--disable-gpu"); // applicable to windows os only options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems options.addArguments("--no-sandbox"); // Bypass OS security model driver = new ChromeDriver(options); driver.get("https://www.wikipedia.org/"); } public void enterText() { WebElement searchBox = driver.findElement(By.id("searchInput")); searchBox.sendKeys("Test"); } public void getDropdownValue() { Select select = new Select(driver.findElement(By.id("searchLanguage"))); List<WebElement> options = select.getOptions(); for (WebElement option : options) { System.out.println(option.getText()); } } public void indexAndValue() { Select select = new Select(driver.findElement(By.id("searchLanguage"))); select.selectByIndex(2); select.selectByValue("cs"); } }