[英]Message “org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the XPath expression” using sendKeys
[英]org.openqa.selenium.InvalidSelectorException: invalid selector SyntaxError: Unexpected token } using XPath through Selenium Java
我正在嘗試單擊以下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 ),因為它找到了目標元素。
我嘗試將整個表達式括在括號中,但沒有用。
你很近。 要在<div>
<span>
標記內標識last()
<span>
標記,您需要從xpath中刪除多余的一對(
和)
。
因此,您需要有效地進行更改:
driver.findElement(By.xpath("(//div[contains(@class, 'js-writeReviewStars')]//span)[last()]")).click();
如:
driver.findElement(By.xpath("//div[contains(@class, 'js-writeReviewStars')]//span[last()]")).click();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.