我正在尝试单击以下HTML中的最后一个跨度元素<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中使用硒,具有以下代码: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),因为它找到了目标元素。我尝试过用括号将整个表达式括起来,但这不起作用。
1 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
你很接近。要识别标记中的标记,您需要从中删除多余的 (
和 )
对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();
添加回答
举报
0/150
提交
取消