How to handle drop down in selenium Webdriver

asked 3 months ago

There are different operations we can perform on the dropdown using a Selenium web driver.

The main operations are:

- Select the dropdown value
- Deselect the dropdown
- Get the options in the dropdown
- Dropdown is Editable or not

Navigate to https://www.qafeast.com/demo,click the dropdown tab.

Press F12 in the keyboard and inspect dropdown.

 

 

Selenium webdriver provides a select class to do the various actions in the dropdown. The actions are to select the dropdown value, deselect the selected option, and get the first option.

 

 

Select the dropdown value:

Selenium webdriver provides a Select class to select the dropdown value based on the Index, by value, and by visible Text. And the dropdown in the HTML page should have Select Tag otherwise we cannot use Select class

 

dropdown_selenium

 

Syntax:

Select drpCountries = new Select(driver.findElement(By.("")));
drpCountries.selectByVisibleText("");

 

Example:

Select drpCountries = new Select(driver.findElement(By.name("countryname")));
drpCountry.selectByVisibleText("ALBANIA ");
drpCountry.selectByIndex(0);
drpCountry.selectByValue("2");

 

Deselect the dropdown:

In Selenium, options in a dropdown can only be deselected if the dropdown allows multiple selections (i.e., it has the multiple attribute). If you attempt to deselect an option in a single-select dropdown, Selenium will throw "You may only deselect options of a multi-select" error. 

Selenium WebDriver provides the following methods in the Select class to deselect options in a multi-select dropdown: 

     1. deselectAll() - Deselects all selected options.
     2. deselectByVisibleText(String text) - Deselects the option with the specified visible text.
     3. deselectByValue(String value) - Deselects the option with the specified value attribute.
     4. deselectByIndex(int index) - Deselects the option at the specified index.

Syntax:

Select drpCountries = new Select(driver.findElement(By.);
drpCountries.deselectByValue("");

 

Example :

Select drpCountries = new Select(driver.findElement(By.name("countryname")));
drpCountries.deselectByValue("");

 

Get the options in the dropdown:   

We may need to verify the options available in the drop-down are the expected ones, to get the options available in the dropdown getOptions() function is used, it will return the list of options. 

Example:

 Select dropDown = new Select(driver.findElement(By.name("countryname")));
 List elementCount = dropDown.getOptions();
 int itemSize = elementCount.size();
 System.out.println(elementCount.size());
 for(int i = 0; i < itemSize ; i++){
         System.out.println(dropDown.getOptions().get(i).getText());
 } 

 

Editable or not:

To verify the dropdown is editable or not isEnabled() is used, it will return the boolean value to determine.

Example:

 

WebElement drop= driver.findElement(By.name("countryname"));
 if(drop.isEnabled()){   
   System.out.println("drop down menu is editable");
 }else{   
   System.out.println("drop down menu is not editable");
 }

 

Using selenium Csharp

 

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApp1
{
    internal class SeleniumTest
    {
        static IWebDriver driver;
        static void Main(string[] args)
        {
           
            driver = new ChromeDriver(@"\\Driver\\chromedriver.exe");
            //Implicit Wait:
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            driver.Navigate().GoToUrl("https://qafeast.com/demo");
            driver.FindElement(By.XPath("//label[text()='Dropdown']")).Click();
           //Select the dropdown value:
            SelectElement drpCountries = new SelectElement(driver.FindElement(By.Name("countryname")));
            drpCountries.SelectByIndex(0);
            drpCountries.SelectByText("ALBANIA");
            drpCountries.SelectByValue("2");
            //Editable or not:
            bool isEnabled = driver.FindElement(By.Name("countryname")).Enabled;
            if (isEnabled)
            {
                Console.WriteLine("drop down menu is editable");
            }
            else
            {
                Console.WriteLine("drop down menu is not editable");
            }
            //You may only deselect options of multiselect is supported
            SelectElement drpCounty = new SelectElement(driver.FindElement(By.Name("countryname")));
            drpCounty.DeselectByValue("");
            //Get the options in the dropdown
            foreach (var ele in drpCounty.Options)
            {
                Console.WriteLine(ele.Text);
            }
            driver.Quit();  
        }
    }
}