我正在嘗試單擊以下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()]}我已經(jīng)檢查了xpath表達(dá)式的有效性,它似乎是正確的(使用 https://www.freeformatter.com/xpath-tester.html),因?yàn)樗业搅四繕?biāo)元素。我嘗試過用括號將整個表達(dá)式括起來,但這不起作用。
1 回答

慕運(yùn)維8079593
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個贊
你很接近。要識別標(biāo)記中的標(biāo)記,您需要從中刪除多余的 (
和 )
對last()
<span>
<div>
斷續(xù)器.
因此,您需要有效地改變:
driver.findElement(By.xpath("(//div[contains(@class, 'js-writeReviewStars')]//span)[last()]")).click();
如:
driver.findElement(By.xpath("//div[contains(@class, 'js-writeReviewStars')]//span[last()]")).click();
添加回答
舉報(bào)
0/150
提交
取消