Saturday, September 14, 2019

Post#50.Handling Dropdown Part-1 (Single Dropdown)


Handle Dropdowns in Selenium Webdriver

Dropdowns are one of the general elements present in any webpage, after buttons and textbox, dropdowns are more frequently available element.
Selenium WebDriver provides Select class, with help of select class we can handle dropdowns on the webpage.

Note: Select class only works if the drop down has select tag.


Steps to select a value in dropdown manually:
1. Click the dropdown
2. Select the required value

Steps to select a value in dropdown in selenium WebDriver:
1. Find the dropdown using findElement
2. Create object for Select class passing dropdown element as parameter
3. Select the value from the dropdown values.

There are two types of dropdowns:
1. Single value Dropdown
2. Multi Value Dropdown

Methods Present in Select Class (All methods are non-static)
  • selectByIndex(int index)
  • selectByValue(String value)
  • selectByVisibleText(String text)
  • getOptions()
  • getFirstSelectedOption()
  • getAllSelectedOptions()
  • isMultiple()
  • deselectByIndex(int index)
  • deselectByValue(String value)
  • deselectByVisibleText(String text)
  • deselectAll()
Single Dropdown
80% of the dropdowns are single value dropdowns, single value dropdowns are normal dropdowns on which you can select only one value, once you choose a value the dropdown shrinks.
Dropdown are formed using select tag in html, if dropdown is not formed with select tag you cannot use this Select Class methods in selenium.
We can use only few methods present in Select class of Selenium WebDriver for Single value Dropdown.
Dropdown Details in selenium WebDriver:

1. Index starts from 0 in dropdowns
2. Value is nothing but 'value' attributes value
3. Value could be different from Visible text(Note : In below dropdown image, value is 1 but the Visible Text is 'Option 1' )
4. Visible text is nothing but text between '>' and '<'

Navigate to Practice page : https://the-internet.herokuapp.com/dropdown


Program to select 1st option in Dropdown using selectByIndex()

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SingleDrodown {
            public static void main(String[] args) throws Exception {
                        // set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/path/geckodriver.exe");
                        // open

                        WebDriver driver = new FirefoxDriver();
                        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
                        // open webpage
                        driver.get("https://the-internet.herokuapp.com/dropdown");
                        // find the dropdown using id
                        WebElement dropdownElement = driver.findElement(By.id("dropdown"));
                        // create object for Select class
                        Select dropdown = new Select(dropdownElement);
                        // select 1st option from the dropdown options
                        dropdown.selectByIndex(1);
                        Thread.sleep(10000);
            }
}

selectByValue()
Sometimes we may need to select an option based on the value attribute, situations like the options may be shuffling or when options are dynamic. 

In such cases we must go either with selectByValue().
Below example show cases how to selct dropdown using selectByValue() method:

package pack4;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SingleDropdownSelectByValue {
            public static void main(String[] args) throws Exception {
                        // set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/path/geckodriver.exe");
                        // open firefox
                        WebDriver driver = new FirefoxDriver();
                        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
                        // open webpage
                        driver.get("https://the-internet.herokuapp.com/dropdown");
                        // find the dropdown using xpath
                        WebElement dropdownElement = driver.findElement(By.xpath("//select[@id='dropdown']"));
                        // create object for Select class
                        Select dropdown = new Select(dropdownElement);
                        // select "1" value option from the dropdown options
dropdown.selectByValue("1");//This will select Option 1
                        Thread.sleep(10000);
            }
}

selectByVisibleText()

Below one is example for selectByVisibleText().

package pack4;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SingleDropdownSelectByValue {
            public static void main(String[] args) throws Exception {
                        // set the geckodriver.exe property
                        System.setProperty("webdriver.gecko.driver", "C:/path/geckodriver.exe");
                        // open firefox
                        WebDriver driver = new FirefoxDriver();
                        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
                        // open webpage
                        driver.get("https://the-internet.herokuapp.com/dropdown");
                        // find the dropdown using xpath
                        WebElement dropdownElement = driver.findElement(By.xpath("//select[@id='dropdown']"));
                        // create object for Select class
                        Select dropdown = new Select(dropdownElement);
                        // select "Option 1" value option from the dropdown options
                        dropdown.selectByVisibleText("Option 1");
                        Thread.sleep(10000);
            }
}


No comments:

Post a Comment