Showing posts with label Select Class. Show all posts
Showing posts with label Select Class. Show all posts

Tuesday, May 27, 2014

How to use Select class to select option from dropdown.

Note- Select class can be used only in one case when dropdown has been created using select tag.


Ex-

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 SelectOption {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://commentit.us/skuline/users/signup");
        WebElement accountType = driver.findElement(By.xpath("//select[@id='UserUserType']"));
        Select sel = new Select(accountType);
        sel.selectByVisibleText("Buyer"); //this will select from dropdown 'Buyer'
        Thread.sleep(2000); //simply wait so that u can see whether it selected option or not
        sel.selectByIndex(0); //this will select 1st option from dropdown '-Account Type-'
        Thread.sleep(2000);
        sel.selectByValue("P");// this will select public which has value="P" in the html tag
    }
}