Thursday, November 17, 2011

How to select value in drop down using Webdriver

How to select value in drop down using Webdriver, By.www.laxmiroy.blogspot.com/


This post will take you through the way you can select the value in a drop down. Webdriver does not have straight forward command "SELECT" which Selenium does.
In below example i will open home page of eBay India and search ipod in Consumer Electronic category. This page has a category drop down in which i will select
Consumer Electronics.
package com.web;
import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DropDownSelection {
 private static WebDriver driver=null;
 @BeforeTest
 @org.testng.annotations.Parameters({"BROWSER"})
 public void setup(String BROWSER){
  if(BROWSER.equals("FF")){
   File profileDir = new File("C:\\nir");
     FirefoxProfile profile = new FirefoxProfile(profileDir);
     profile.setPreference("general.useragent.override", "same user agent string as above");
     driver= new FirefoxDriver(profile);
   System.out.println("Broser Selected " + BROWSER); 
  }else if(BROWSER.equals("IE")){
   System.out.println("Broser Selected " + BROWSER);
   driver = new InternetExplorerDriver();
  }else if(BROWSER.equals("HU")){
   System.out.println("Broser Selected " + BROWSER);
   driver =new HtmlUnitDriver();
  } else
  {
   System.out.println("Broser Selected " + BROWSER);
   driver = new ChromeDriver();
  }
 }
 @Test
 public void testDropDownSelection() throws InterruptedException{
 
  driver.get("http://www.ebay.in");
  WebElement search = driver.findElement(By.name("_nkw"));
  search.sendKeys("ipod");

   List dd = driver.findElement(By.name("_sacat")).findElements(By.tagName("option"));
   for (WebElement option : dd) {
    System.out.println(option.getText());
    if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
     option.click();
     break;
    }
   }
   driver.findElement(By.xpath("//input[@value='Search']")).submit();
   Thread.sleep(5000);
   driver.getPageSource().contains("results found");
  driver.quit();
 }
}

TestNG.xml
Using TestNG suite we will run the test case.
<?xml version="1.0" encoding="UTF-8"?>
<suite name="webDriver" parallel="tests">
 <listeners>
  <listener class-name="org.uncommons.reportng.HTMLReporter" />
  <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
 </listeners>
 <test name="WebDriverDemo Witn FF" preserve-order="true">
  <parameter name="BROWSER" value="FF" />
  <classes>
   <class name="com.web.DropDownSelection" />
  </classes>
  </test>
  </b>
</suite>
If above code does not work with you configuration just try another way given below.
 List  options = driver.findElements(By.tagName("option"));
  for (WebElement option : options) {
   if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
    option.click();
   }
      }

1 comment:

  1. Hi I had just started automation in Selenium,can you please tell me the difference between getText and getValue ?

    ReplyDelete