How to select values from drop down list ?

In this post we are going to automate an Drop down list in two ways :

Method 1 : By using select class from selenium webdriver

Method 2 : By using sendkeys method from selenium webdriver

Let’s use chropath to inspect Drop down box and identify the WebElement locator.

As it is visible that drop down box has name, hence taking the name of that WebElement (By.name(“cars”)). Let’s use this Name locator to Select the drop down list.

Method 1 : By using select class from selenium webdriver

Steps to be Followed :

  1. Start writing code in an Java file.
  2. First let’s launch the browser (say using chrome here in the example).
  3. Enter the Appliction under test(AUT) url in the browser.

driver.get(“http://qacreators.com/FormToFill.html”);

  1. Maximize the browser window.

driver.manage().window().maximize();

  1. To identify the Drop down list we are using By.name(“cars”) (i.e name locator)

Let’s discuss about this line of code :

WebElement dropDownList = driver.findElement(By.name(“cars”));

–> driver : is the webdriver object.

–> findElement() : Used to identify or locate the elements to perform the operation.

–> By.name(“cars”): By.name helps the findElement() to find the elements with name locator. Here in this example we are using (name = “cars”) to identify the drop down list.

–> selectByVisibleText() : is an command from selenium webdriver used to select the list option using the specified List option label from the multi selection box field.

–> WebElement dropDownList : Reference of the dropDownList element is stored into dropDownList variable.

  1. To Select value from drop down list use select class.

Select select = new Select(dropDownList);

select.selectByVisibleText(“Volvo”);

  1. Execute the script by right click run as –> Java application.
  2. Once the script is executed we can observe the drop down box is selected with Volvo option.
  3. Finally close the browser window.
    Method 2 : By using sendkeys from selenium webdriver

Steps to be Followed :

  1. Start writing code in an Java file.
  2. First let’s launch the browser (say using chrome here in the example).
  3. Enter the Appliction under test(AUT) url in the browser.

driver.get(“http://qacreators.com/FormToFill.html”);

  1. Maximize the browser window.

driver.manage().window().maximize();

  1. To identify the Drop down list we are using By.name(“cars”) (i.e name locator)

Let’s discuss about this line of code :

WebElement dropDownList = driver.findElement(By.name(“cars”));

–> driver : is the webdriver object.

–> findElement() : Used to identify or locate the elements to perform the operation.

–> By.name(“cars”): By.name helps the findElement() to find the elements with name locator. Here in this example we are using (name = “cars”) to identify the drop down list.

–> SendKeys() : Webdriver command used to enter data into the text box field.

–> WebElement dropDownList : Reference of the dropDownList element is stored into dropDownList variable.

  1. To Select value from drop down list use sendkeys method, to enter the text value. Here we are using WebElement reference i.e dropDownList

dropDownList.sendKeys(“Volvo”);

  1. Execute the script by right click run as –> Java application.
  2. Once the script is executed we can observe the drop down box is selected with Volvo option.
  3. Closing the browser window.

Leave a Comment

Your email address will not be published. Required fields are marked *