Friday 12 July 2013

Select value from drop down list using WebDriver

WebDriver provides the class named 'Select' which is in the package 'org.openqa.selenium.support.ui' and below methods to choose the value from the drop down list.

  • selectByVisibleText("text")
  • selectByValue("value")
  • selectByIndex(index number)

Note: This is applicable only if the drop down list implemented by <select> tag  in html as below,

Let us consider the face book sign-up page, which has the dropdown lists in the Birthday field as below,



Example Program:


It chooses month for the Birthday field.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class dropDownList{
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

                driver.get("http://www.facebook.com/");
              
                driver.manage().window().maximize(); 
// Chooses Apr month
Select brtmonth = new Select(driver.findElement(By.id("birthday_month")));
brtmonth.selectByVisibleText("Apr");
}

}




1 comment:

  1. Appium Training in chennai
    We provides Best Appium Training in Chennai with real time project assistance by our leading Appium mobile App Automation testing architect
    For Free Live Demo @ Call to 8122241286.
    Appium ,Selenium,Mobile Automation Testiing using Android and IOS
    www.thecreatingexperts.com
    Appium training in chennai

    ReplyDelete