November 25, 2010

Print values in the dropdown using Xpath

The below script explains you how to get the values in a dropdown and print the values to a notepad or excel sheet using the xpath...




import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;


public class Dropdowngooglexpath {
public Selenium selenium;
public SeleniumServer seleniumserver;

  @BeforeClass
  public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testDropdowngoogle()throws Exception {
selenium.open("http://www.google.com");
//click on Advanced Search Window
    selenium.click("link=Advanced Search");    
    selenium.waitForPageToLoad("5000");
     //Print all the available options from the results dropdown in the google advanced search page
//here i am using xpath to identify the element
    String[] option = selenium.getSelectOptions("xpath=/html/body/table[2]/tbody/tr/td/table/tbody/tr/td/div/form/div/table[2]/tbody/tr[1]/td[2]/select");      
    //The above command returns a array of strings(options)
   //write results to excel and note pad
    File file = new File("C:/Dropdownvalues.xls");//excel
    File file1 = new File("C:/Dropdownvalues.txt");//notepad
    BufferedWriter out = new BufferedWriter(new FileWriter(file));//excel
    BufferedWriter out1 = new BufferedWriter(new FileWriter(file1));//notepad
    out.write("Options in drop down\n");//Excel
    out1.write("Options in drop down\n");//notepad
    for (int i = 0; i < option.length; i++) {
        System.out.println("Option: " + i + " is" + option[i]);
        out.write(""+option[i]);
        out.newLine();      
        out1.write(""+option[i]);
        out1.newLine();      
    }
    out.close();//excel
    out1.close();//notepad
  }
 @AfterClass
 public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();

 }
 }

2 comments:

  1. Hi Naga here i am using Junit instead of testng but it shows below error
    "com.thoughtworks.selenium.SeleniumException: ERROR: Element xpath=/html/body/table[2]/tbody/tr/td/table/tbody/tr/td/div/form/div/table[2]/tbody/tr[1]/td[2]/select not found
    at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)
    at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)
    at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:275)
    at com.thoughtworks.selenium.HttpCommandProcessor.getStringArray(HttpCommandProcessor.java:284)
    at com.thoughtworks.selenium.DefaultSelenium.getSelectOptions(DefaultSelenium.java:520)
    at newchecking.testNew1(newchecking.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:249)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)"

    ReplyDelete
  2. Hi Archana,

    It seems the selenium is unable to identify XPATH. Please use Firebug and write new xpath there..

    If you ahev any questiosn psl send mail to nagaselenium@gmail.com

    ReplyDelete