gpt4 book ai didi

java - Selenium WebDriver 在选择 xpath 时抛出错误

转载 作者:行者123 更新时间:2023-11-29 07:06:02 24 4
gpt4 key购买 nike

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: 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/fxdriver@googlecode.com/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.html Build info: version: '2.37.0', revision: 'a7c61cbd68657e133ae96672cf995890bad2ee42', time: '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 + "']"));

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

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

关于java - Selenium WebDriver 在选择 xpath 时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19694631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com