【发布时间】:2019-08-30 11:57:43
【问题描述】:
我正在尝试单击以下 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),因为它找到了目标元素。
我尝试将整个表达式括在括号中,但没有成功。
【问题讨论】:
标签: java selenium selenium-webdriver xpath xpath-1.0