Selenium WebDriver在选择xpath时抛出错误

1 java selenium xpath selenium-webdriver

driver.findElement(By.xpath("//input[@type="+"checkbox"+"]/following-sibling:://td[contains(text(),"+"template"+"]"))
Run Code Online (Sandbox Code Playgroud)

我的HTML就像这样

<tr>
<td class="tablecontent">
<input type="checkbox" value="59781" name="templateIds">
</td>`enter code here`
<td class="tablecontent"> test11 </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

org.openqa.selenium.InvalidSelectorException:给定的选择器//输入[@type = checkbox]/following-sibling :: // td [contains(text(),template]无效或不会产生WebElement.发生以下错误:InvalidSelectorError:由于以下错误,无法找到具有xpath表达式的元素//输入[@type = checkbox]/following-sibling :: // td [contains(text(),template]:[Exception] ..."表达式不是合法表达式."代码:"12"nsresult:"0x805b0033(SyntaxError)"location:"file:/// C:/ Users/sanjdash/AppData/Local/Temp/anonymous3529970525380845680webdriver-profile /extensions/fxdriver@googlecode.com/components/driver_component.js行:5956"]命令持续时间或超时:72毫秒有关此错误的文档,请访问: http://seleniumhq.org/exceptions/invalid_selector_exception.html构建信息:版本:'2.37.0',修订版:'a7c61cbd68657e133ae96672cf995890bad2ee42',时间:'2013-10-18 09:51:02'

Yi *_*eng 5

看起来你搞砸了引号.在XPath中使用单引号来避免这样的问题.

// if template is the text within your XPath
driver.findElement(By.xpath("//input[@type='checkbox']/following-sibling:://td[contains(text(), 'template']"));

// if template is your variable, then it should be
driver.findElement(By.xpath("//input[@type='checkbox']/following-sibling:://td[contains(text(), " + template + "']"));
Run Code Online (Sandbox Code Playgroud)

此外,请仔细阅读错误,它已经告诉您需要的足够信息.如您所见,消息中的选择器中没有引号.

给定的选择器//输入[@type = checkbox]/following-sibling :: // td [contains(text(),template]无效或不会产生WebElement.