In this post we could learn about to select a option from a dropdown list.
Here is the very basic program to select a option from Blog Archive dropdown available on hello selenium blog homepage:
Here is the very basic program to select a option from Blog Archive dropdown available on hello selenium blog homepage:
Java Source code:
package com.helloselenium.selenium.test;
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.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class GetDropdownOption{
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://helloselenium.blogspot.com");
(new WebDriverWait(driver, 60)).until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver newDriver) {
return newDriver.findElement(By.id("BlogArchive1_ArchiveMenu"));
}
});
Select archiveList = new Select(driver.findElement(By.id("BlogArchive1_ArchiveMenu")));
archiveList.selectByVisibleText("Apr 2013 (38)");
String selectedValue = archiveList.getFirstSelectedOption().getText();
System.out.println(selectedValue);
driver.quit();
}
}
Detailed explanation for the above program is as follows:
Above code is package structure for current program file.
Following code is the required packages for Selenium Webdriver.
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.ExpectedCondition; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait;
Above both required packages are available in downloaded selenium server jar file.
Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
driver.get("http://helloselenium.blogspot.com");
driver.navigate().to("http://helloselenium.blogspot.com");
(new WebDriverWait(driver, 60)).until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver newDriver) {
return newDriver.findElement(By.id("BlogArchive1_ArchiveMenu"));
}
});
Select archiveList = new Select(driver.findElement(By.id("BlogArchive1_ArchiveMenu")));
archiveList.selectByVisibleText("Apr 2013 (38)");
String selectedValue = archiveList.getFirstSelectedOption().getText();
System.out.println(selectedValue);
driver.quit();
driver.close();
0 Comments
What would you like to add in my list? I look forward to reading your comments below.