gpt4 book ai didi

java - org.openqa.selenium.InvalidSelectorException : invalid selector SyntaxError: Unexpected token } using XPath through Selenium Java

转载 作者:行者123 更新时间:2023-11-30 01:52:32 25 4
gpt4 key购买 nike

我正在尝试单击以下 HTML 中的最后一个 span 元素

<div class="rating rating-set js-ratingCalcSet" >
<div class="rating-stars js-writeReviewStars">
<span class="js-ratingIcon glyphicon glyphicon-star fh"></span>
<span class="js-ratingIcon glyphicon glyphicon-star lh"></span>
...
<span class="js-ratingIcon glyphicon glyphicon-star fh"></span>
<span class="js-ratingIcon glyphicon glyphicon-star lh"></span>
</div>
</div>

在Java中使用Selenium,代码如下:

driver.findElement(By.xpath("(//div[contains(@class, 'js-writeReviewStars')]//span)[last()]")).click(); 

这会返回一个异常:

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression (//div[contains(@class, 'js-writeReviewStars')]//span)[last()] because of the following error:
SyntaxError: Unexpected token }
...
*** Element info: {Using=xpath, value=(//div[contains(@class, 'js-writeReviewStars')]//span)[last()]}

我已经检查了 xpath 表达式的有效性,它似乎是正确的(使用 https://www.freeformatter.com/xpath-tester.html ),因为它找到了目标元素。

我尝试过将整个表达式括在括号中,但没有成功。

最佳答案

你们已经很接近了。识别last() <span> <div> 内的标签您需要删除额外的一对 ( 标签 ) 来自 .

因此,您需要改变:

driver.findElement(By.xpath("(//div[contains(@class, 'js-writeReviewStars')]//span)[last()]")).click(); 

如:

driver.findElement(By.xpath("//div[contains(@class, 'js-writeReviewStars')]//span[last()]")).click(); 

关于java - org.openqa.selenium.InvalidSelectorException : invalid selector SyntaxError: Unexpected token } using XPath through Selenium Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55588599/

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