HOW TO GET SELECTED 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.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;
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 and Webdriver wait.
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();
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 wait for dropdown list.
(new WebDriverWait(driver, 60)).until(new ExpectedCondition<WebElement>() {
    public WebElement apply(WebDriver newDriver) {
     return newDriver.findElement(By.id("BlogArchive1_ArchiveMenu"));
    }
   });
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)");
Using below code you can get the selected option from the dropdown.
String selectedValue = archiveList.getFirstSelectedOption().getText();
Below code is used to print the stored value.
System.out.println(selectedValue);
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