【问题标题】:Selenium Webdriver: how to use xpath filter?Selenium Webdriver:如何使用 xpath 过滤器?
【发布时间】:2015-06-12 16:48:30
【问题描述】:

在此处使用 HTML 表格的情况下,我正在尝试遵循 Selenium Webdrive 基本教程

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

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

切萨雷

【问题讨论】:

    标签: java selenium xpath webdriver


    【解决方案1】:

    您的 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

    【讨论】:

    • 响应速度非常快!!!它现在正在工作......当我为 HTML 元素捕获 xpath 时出现问题......我现在已经检查过,一切正常。非常感谢!
    【解决方案2】:

    正如他们在练习测试场景中给出的那样,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;
    

    公开课练习表_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();
    
        }
    
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      相关资源
      最近更新 更多