在 Xpath 表达式上使用 Selenium Java 中的参数
Using arguments in Selenium Java on Xpath Expression
我想使用参数 'option' 作为 Xpath 表达式的 findElement 的一部分:
public HomePage clickSizeOption (String option){
driver.findElement(By.xpath("span[contains(@class, 'checkmark') and text()= option]")).click();
return this;
}
但是java将整个xpath视为一个字符串。
HTML 上下文:
您应该可以为此使用 format() 方法。我还注意到您的 xpath 不是以“//”开头的。
Afaik xpath 应该始终以“//”开头,除非您从 html 元素开始。
试试这个
driver.findElement(By.xpath(String.format("//span[contains(@class, 'checkmark') and text()= %s]", option)).click();
我想使用参数 'option' 作为 Xpath 表达式的 findElement 的一部分:
public HomePage clickSizeOption (String option){
driver.findElement(By.xpath("span[contains(@class, 'checkmark') and text()= option]")).click();
return this;
}
但是java将整个xpath视为一个字符串。
HTML 上下文:
您应该可以为此使用 format() 方法。我还注意到您的 xpath 不是以“//”开头的。
Afaik xpath 应该始终以“//”开头,除非您从 html 元素开始。
试试这个
driver.findElement(By.xpath(String.format("//span[contains(@class, 'checkmark') and text()= %s]", option)).click();