我想通過 Xpath 運算式使用引數“選項”作為 findElement 的一部分:
public HomePage clickSizeOption (String option){
driver.findElement(By.xpath("span[contains(@class, 'checkmark') and text()= option]")).click();
return this;
}
但是java將整個by xpath視為一個字串。
HTML 背景關系:

uj5u.com熱心網友回復:
您應該能夠為此使用 format() 方法。我還注意到您的 xpath 不是以“//”開頭的。
Afaik xpath 應始終以“//”開頭,除非您從 html 元素開始。
試試這個
driver.findElement(By.xpath(String.format("//span[contains(@class, 'checkmark') and text()= %s]", option)).click();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482291.html
上一篇:用硒從彈出視窗中按下一個按鈕
