Selenium Webdriver tutorial : How To Select Dropdown Value With Example

In my earlier tutorial of selenium webdriver software testing tool, we learn few basic action commands of selenium WebDriver like HOW TO CLICK ON BUTTON, IMPLICIT WAIT, EXPLICIT WAIT etc. THIS LINK will show you more different examples of basic action commands of selenium webdriver software testing tool. As you know, Drop down is also essential element of any software web application page. So it is important to know that how to control or take action on drop down list like selecting specific value from drop down list using selenium webdriver/selenium 2.

Create selenium webdriver data driven framework from scratch @This Page.

If you know, In selenium IDE software testing tool we can select value from drop down list using "select" or "addSelection" command. Now let me give you example of how to select value from drop down list in webdriver.

In bellow given example, First imported webdriver package "org.openqa.selenium.support.ui.Select" to get support of webdriver class "Select".
import org.openqa.selenium.support.ui.Select;

Now we can use "Select" class to identify drop down from web page by writing bellow given syntax.
Select mydrpdwn = new Select(driver.findElement(By.id("Carlist")));

Now we can select any value from selected drop down as bellow.
mydrpdwn.selectByVisibleText("Audi");

Full Example to select value from drop down of software applications's web page is as bellow.

EXAMPLE
package junitreportpackage;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Mytesting {
 WebDriver driver = null;
 
 @Before
 public void beforetest() {
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.com/2014/01/textbox.html");
 }
 
 @After
 public void aftertest() {
  driver.quit();
  
 }
 
 @Test
 public void test () throws InterruptedException 
 {  
  driver.findElement(By.id("text1")).sendKeys("My First Name");

  //Selecting value from drop down using visible text
  Select mydrpdwn = new Select(driver.findElement(By.id("Carlist")));
  mydrpdwn.selectByVisibleText("Audi");
  WebDriverWait wait = new WebDriverWait(driver, 15);
  wait.until(ExpectedConditions.elementToBeClickable(By.id("text2")));
 }
   
}

Same thing we can do for list box to select option from list box by visible text. View my previous post to know how to select value by index from drop down.

8 comments:

  1. Please help me for this tutorial .
    When i run to wait.until(ExpectedConditions.elementToBeClickable(By.id("text2"))); ,it raises error : Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"id","selector":"Text2"}

    ReplyDelete
    Replies
    1. use this link http://only-testing-blog.blogspot.in/2014/01/textbox.html
      you might've used a different url

      Delete
  2. Hi,
    I am learning selenium webdriver. Yesterday I spent whole day trying to output the values of a dropdown box from a site called sulekha.com I tried all the examples in software ytesting tutorial post but couldn't get which locator to use to find the elements in the dropdown. Please help. Thanks

    ReplyDelete
  3. Selenium support Kendo Dropdown ?

    ReplyDelete
  4. i dnt think so, selenium doesnt support kendo grids/dropdowns at all. even im stuck with one major aspect. if any one knows the way. please guide.

    ReplyDelete
  5. WebDriver wd = new FirefoxDriver();

    wd.get("http://www.sulekha.com/");
    wd.findElement(By.xpath("//button[@type='button']")).click();

    wd.findElement(By.xpath(".//*[@id='location-dropdown']/a")).click();
    wd.findElement(By.xpath(".//*[@class='ui-menu-item']")).getText();

    List citylist = wd.findElements(By.xpath(".//*[@class='ui-menu-item']"));

    for (WebElement city: citylist){
    System.out.println("WebElement : " + city.getText() );
    }

    ReplyDelete
  6. WebDriver Driver = new FirefoxDriver();
    // Driver.manage().window().maximize();
    Driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    Driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
    Driver.get("http://www.sulekha.com/");
    Driver.findElement(By.xpath("//*[@id='location-dropdown']/a")).click();
    Thread.sleep(1000);
    Driver.findElement(By.xpath("//*[@id='lcf-location']")).sendKeys("Mumbai");
    Thread.sleep(1000);
    Driver.findElement(By.xpath("//*[@id='lcf-location']")).sendKeys(Keys.ENTER);

    ReplyDelete
  7. I spent my two days on selecting value from drop down using every method mention here through index,value,visible text kindly help me .

    ReplyDelete