Selenium Webdriver:如何使用xpath过滤器?

时间:2015-06-12 07:39:06

标签: java selenium xpath webdriver

我想在这里使用HTML表格时遵循Selenium Webdrive Basic Tutorial

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

该页面中“练习练习1”的代码不起作用:问题似乎是关于此处的xpath过滤器

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

在这里

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

示例代码中使用的页面是这个

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

我尝试使用Firebug更改直接从页面中提取xpath的代码,我的新代码如下

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();

}

}

尝试执行错误仍然是

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]"}

我在Windows 7上使用Eclipse Luna

有什么建议吗?提前谢谢你......

切萨雷

2 个答案:

答案 0 :(得分:2)

你的xpath错了。我看到你尝试从第二行/第一列获取内容(如果你没有计算标题)。请在http://www.toolsqa.com/automation-practice-table/页面上尝试以下代码:

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

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

如果您运行getText(),则会返回以下值:Saudi Arabia

答案 1 :(得分:1)

正如他们在练习测试场景中给出的那样,xpath已被更改。并且xpath中的ID或className可能会更改。所以根据更新的HTML代码更改xpath。

我在这里改变了一切:

 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();

    }

   }