HOW TO SELECT A DROPDOWN VALUE USING SELENIUM WEBDRIVER?

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:




Java Source code:

package com.helloselenium.selenium.test;

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 SelectDropdownOption{

 public static void main(String[] args) { 

  WebDriver driver = new FirefoxDriver();

  driver.get("http://helloselenium.blogspot.com");

  Select archiveList = new Select(abhiDriver.findElement(By.id("BlogArchive1_ArchiveMenu")));

  archiveList.selectByVisibleText("Apr 2013 (38)");
  
  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;
Following code is the required packages for Firefox browser.
import org.openqa.selenium.firefox.FirefoxDriver;
Following code is the required packages for select type of variables.
import org.openqa.selenium.support.ui.Select;
Above both required packages are available in downloaded selenium server jar file.
Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
Following code is to open the hello selenium blog in browser.
driver.get("http://helloselenium.blogspot.com");




You can also use the following code is to open the hello selenium blog in browser.
driver.navigate().to("http://helloselenium.blogspot.com");
Below code is used to locate the dropdown select list on the webpage.
Select archiveList = new Select(driver.findElement(By.id("BlogArchive1_ArchiveMenu")));
Using below code you can select the visible option from the dropdown.
archiveList.selectByVisibleText("Apr 2013 (38)");
Following code is to quit the Firefox driver instance.
driver.quit();
You can also use the following code is to close the Firefox driver instance.
driver.close();



Post a Comment

0 Comments