Java Selenium WebDriver 在选择 xpath 时抛出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19694631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:27:29  来源:igfitidea点击:

Selenium WebDriver throwing error while selecting the xpath

javaxpathseleniumselenium-webdriver

提问by user2939258

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

My HTML is like this

我的 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: The given selector //input[@type=checkbox]/following-sibling:://td[contains(text(),template] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //input[@type=checkbox]/following-sibling:://td[contains(text(),template] because of the following error: [Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "file:///C:/Users/sanjdash/AppData/Local/Temp/anonymous3529970525380845680webdriver-profile/extensions/[email protected]/components/driver_component.js Line: 5956"] Command duration or timeout: 72 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.htmlBuild info: version: '2.37.0', revision: 'a7c61cbd68657e133ae96672cf995890bad2ee42', time: '2013-10-18 09:51:02'

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

回答by Yi Zeng

Looks like your messed up with quotes. Use single quotes in XPath to avoid issues like this.

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

Also, please read the error carefully, it tells enough information you need already. As you can see, there are no quotes in the selector in the message.

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

The given selector //input[@type=checkbox]/following-sibling:://td[contains(text(),template] is either invalid or does not result in a WebElement.

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

回答by user3638751

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

produces a wrong string. Try from python:

产生错误的字符串。从 python 尝试:

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

You should surround checkbox and template with quotes to get

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

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

or

或者

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

use

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

or

或者

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

回答by Robert Hoeppner

Your "contains" function has no closing bracket and needs the right quoting.

您的“包含”函数没有右括号,需要正确引用。

If CHECKBOX and TEMPLATE are Strings, try this:

如果 CHECKBOX 和 TEMPLATE 是字符串,试试这个:

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

if they are variables, try this:

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

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