Selenium Webdriver:如何使用 xpath 过滤器?
Selenium Webdriver: how to use xpath filter?
在此处使用 HTML 表的情况下,我正在尝试遵循 Selenium Webdrive 基本教程
http://www.toolsqa.com/selenium-webdriver/handling-tables-selenium-webdriver/
该页面中 "Practice Exercise 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
有什么建议吗?提前谢谢你...
切萨雷
你的xpath是错误的。如我所见,您尝试从第二个 row/first 列获取内容(如果您不计算 headers)。在 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()
它将 return 以下值: Saudi Arabia
正如他们在练习测试场景中给出的那样,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();
}
}
在此处使用 HTML 表的情况下,我正在尝试遵循 Selenium Webdrive 基本教程
http://www.toolsqa.com/selenium-webdriver/handling-tables-selenium-webdriver/
该页面中 "Practice Exercise 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有什么建议吗?提前谢谢你...
切萨雷
你的xpath是错误的。如我所见,您尝试从第二个 row/first 列获取内容(如果您不计算 headers)。在 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()
它将 return 以下值: Saudi Arabia
正如他们在练习测试场景中给出的那样,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();
}
}