Selenium WebDriver 在选择 xpath 时抛出错误


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

我的 HTML 是这样的

<tr>
<td class="tablecontent">
<input type="checkbox" value="59781" name="templateIds">
</td>`enter code here`
<td class="tablecontent"> test11 </td>
</tr>

org.openqa.selenium.InvalidSelectorException: 给定的选择器 input[@type=复选框]/follow-sibling:://td[contains(text(),template] 无效或未生成 WebElement。以下 发生错误:无效选择器错误:无法找到具有 XPath表达式 input[@type=复选框]/follow-sibling:://td[contains(text(),template] 由于以下错误:[异常..."表达不是 法律表达。 代码:"12" ns结果:"0x805b0033(语法错误)" 位置: "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'

看起来你的引号搞砸了。在 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 + "']"));

另外,请仔细阅读错误,它已经提供了您需要的足够信息。如您所见,消息中的选择器中没有引号。

给定的选择器 input[@type=复选框]/follow-sibling:://td[contains(text(),template] 无效或未生成 WebElement。

你的"contains"函数没有右括号并且需要正确的报价。

如果复选框和模板是字符串,请尝试以下操作:

driver.findElement(By.xpath("//input[@type='checkbox']/following-sibling:://td[contains(text(),'template')]"))

如果它们是变量,请尝试以下操作:

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

生成错误的字符串。尝试使用python:

"//input[@type="+"checkbox"+"]/following-sibling:://td[contains(text(),"+"template"+"]"
'//input[@type=checkbox]/following-sibling:://td[contains(text(),template]'

您应该用引号将复选框和模板括起来以获得

"//input[@type="checkbox"]/following-sibling:://td[contains(text(),"template"]"

"//input[@type='checkbox']/following-sibling:://td[contains(text(),'template']"

"//input[@type="+"'checkbox'"+"]/following-sibling:://td[contains(text(),"+"'template'"+"]"

"//input[@type='{type}']/following-sibling:://td[contains(text(),'{text}']".format(type='checkbox',text='template')

相关内容

最新更新