使用模板时,xpath不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用模板时,xpath不起作用相关的知识,希望对你有一定的参考价值。

有人知道为什么xpath不起作用?当我使用“LG”时,不是%s的模板一切正常。

public static void check(){
    String path=""//span[@class='schema-filter__checkbox-text' and text()='%s']"";
    String way = String.format(path, "LG");
    driver.findElement(By.xpath(way); - not working
}
答案

该方法无效,因为我设置了不正确的属性。 public class Page {static WebDriver driver;

public static void main(String[] args) {
    System.setProperty("webdriver.gecko.driver", "C:\Users\....\drivers\geckodriver.exe");
    driver=new FirefoxDriver();    - I set WebDriver driver=new FirefoxDriver()  in the beginning.

..... ..... }

并且还应删除模板中的“”。

public static void f(){
    String path="//span[@class='schema-filter__checkbox-text' and text()='%s']";
    String way = String.format(path, "LG");
    System.out.println(driver.findElement(By.xpath(way)).getText());
}

以上是关于使用模板时,xpath不起作用的主要内容,如果未能解决你的问题,请参考以下文章