Selenium Webdriver: how to use xpath filter?

2.1k Views Asked by At

I'm trying to follow the Selenium Webdrive Basic Tutorial in the case of using HTML tables here

http://www.toolsqa.com/selenium-webdriver/handling-tables-selenium-webdriver/

The code of "Practice Exercise 1" in that page doesn't work: the problem seems to be about the xpath filter here

String sCellValue = driver.findElement(By.xpath(".//*[@id='post-1715']/div/div/div/table/tbody/tr[1]/td[2]")).getText();

and here

driver.findElement(By.xpath(".//*[@id='post-1715']/div/div/div/table/tbody/tr[1]/td[6]/a")).click(); 

The page used in the sample code is this one

http://www.toolsqa.com/automation-practice-table/

I've tried to change the code extracting the xpath directly form the page using Firebug and my new code is the following

package practiceTestCases;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class PracticeTables_00 {

private static WebDriver driver = null;

public static void main(String[] args) {

        driver = new FirefoxDriver();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("http://www.toolsqa.com/automation-practice-table");

        //Here we are storing the value from the cell in to the string variable

        String sCellValue = driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[2]")).getText();

        System.out.println(sCellValue);

        // Here we are clicking on the link of first row and the last column

        driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[6]/a")).click();        

        System.out.println("Link has been clicked otherwise an exception would have thrown");

        driver.close();

}

}

Trying to execute the error is still

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[2]"}

I'm using Eclipse Luna on Windows 7

Any suggestions? Thank you in advance ...

Cesare

2

There are 2 best solutions below

1
On BEST ANSWER

Your xpath is wrong. As I see you try to get the content from the second row/first column (if you doesn't count the headers). Try the following code on http://www.toolsqa.com/automation-practice-table/ page:

/html/body/div[2]/div[3]/div[2]/div/div/table/tbody/tr[2]/td[1]

or

//*[@id='content']/table/tbody/tr[2]/td[1]

If you run getText() it will return the following value: Saudi Arabia

0
On

As they given in Practice Test scenario, that xpath has been changed. And ID or className in the xpath might be changed. so change the xpath as per updated HTML code.

Here i have changed everything:

 import java.util.concurrent.TimeUnit;

 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver;

public class PracticeTables_00 {

private static WebDriver driver = null;

public static void main(String[] args) {

    driver = new FirefoxDriver();

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    driver.get("http://www.toolsqa.com/automation-practice-table");

    // Here we are storing the value from the cell in to the string variable

    String sCellValue = driver
            .findElement(
                    By.xpath(".//*[@id='content']/table/tbody/tr[1]/td[2]"))
            .getText();

    System.out.println(sCellValue);

    // Here we are clicking on the link of first row and the last column

    driver.findElement(
            By.xpath(".//*[@id='content']/table/tbody/tr[1]/td[6]/a"))
            .click();

    System.out
            .println("Link has been clicked otherwise an exception would have thrown");

    driver.close();

    }

   }