【发布时间】: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