問題描述
xPath 在使用 for‑each 循環變量時找不到選擇器,但可以正常工作 (xPath not finding selector when using for‑each loop variable, but works otherwise)
塊包含以下內容,明確說明了值,它可以工作:</p>
ArrayList<String> fieldList = new ArrayList<String>();
fieldList.add("Street");
fieldList.add("City");
fieldList.add("Country");
我做錯了什麼?非常感謝。
參考解法
方法 1:
You are forgetting to quote your strings in the XPath expression. Add single quotes around cField
:
waitForElement.until(ExpectedConditions.elementToBeClickable(
By.xpath("//td[following‑sibling::td[2] = '" + cField + "']/input")));
// quotes added here ‑‑‑^ and here ‑‑‑^
WebElement checkBox =
driver.findElement(By.xpath("//td[following‑sibling::td[2] = '" + cField + "']/input"));
// quotes added here ‑‑‑^ and here ‑‑‑^
(by Mathomatic、acdcjunior)