0

在网页上,我有一个评级星星反馈,当我将鼠标悬停在星星上时它们变成黄色。我有 5/6 相似的 div,相同的类和配置。如果我悬停并单击第 4 颗星星,它们都变成黄色。我想做的是定位第四颗星并点击它来设置评级。我尝试使用动作链和 xpath,但它不起作用。以下是星星的html:

<div class="rating-box-wrapper" style="height: 35px;">
<svg viewBox="0 0 34 32" preserveAspectRatio="none" zing-touch="" class="rvs-star-svg" width="38" height="35.72" style="touch-action: manipulation; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><!----><!----><!----><g><path d="M6.37 32l3.972-12.215-10.417-7.569h12.89l3.972-12.215 3.972 12.215h12.89l-10.417 7.569 3.972 12.215-10.417-7.569zM16.787 22.557l7.569 5.471-2.848-8.843 7.569-5.471h-9.368l-2.848-8.843-2.848 8.843h-9.368l7.569 5.471-2.848 8.843z" fill="#4ae0e1"></path></g><!----><!----></svg>
<svg viewBox="0 0 34 32" preserveAspectRatio="none" zing-touch="" class="rvs-star-svg" width="38" height="35.72" style="touch-action: manipulation; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><!----><!----><!----><g><path d="M6.37 32l3.972-12.215-10.417-7.569h12.89l3.972-12.215 3.972 12.215h12.89l-10.417 7.569 3.972 12.215-10.417-7.569zM16.787 22.557l7.569 5.471-2.848-8.843 7.569-5.471h-9.368l-2.848-8.843-2.848 8.843h-9.368l7.569 5.471-2.848 8.843z" fill="#4ae0e1"></path></g><!----><!----></svg>
<svg viewBox="0 0 34 32" preserveAspectRatio="none" zing-touch="" class="rvs-star-svg" width="38" height="35.72" style="touch-action: manipulation; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><!----><!----><!----><g><path d="M6.37 32l3.972-12.215-10.417-7.569h12.89l3.972-12.215 3.972 12.215h12.89l-10.417 7.569 3.972 12.215-10.417-7.569zM16.787 22.557l7.569 5.471-2.848-8.843 7.569-5.471h-9.368l-2.848-8.843-2.848 8.843h-9.368l7.569 5.471-2.848 8.843z" fill="#4ae0e1"></path></g><!----><!----></svg>
<svg viewBox="0 0 34 32" preserveAspectRatio="none" zing-touch="" class="rvs-star-svg" width="38" height="35.72" style="touch-action: manipulation; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><!----><!----><!----><g><path d="M6.37 32l3.972-12.215-10.417-7.569h12.89l3.972-12.215 3.972 12.215h12.89l-10.417 7.569 3.972 12.215-10.417-7.569zM16.787 22.557l7.569 5.471-2.848-8.843 7.569-5.471h-9.368l-2.848-8.843-2.848 8.843h-9.368l7.569 5.471-2.848 8.843z" fill="#4ae0e1"></path></g><!----><!----></svg>
<svg viewBox="0 0 34 32" preserveAspectRatio="none" zing-touch="" class="rvs-star-svg" width="38" height="35.72" style="touch-action: manipulation; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><!----><!----><!----><g><path d="M6.37 32l3.972-12.215-10.417-7.569h12.89l3.972-12.215 3.972 12.215h12.89l-10.417 7.569 3.972 12.215-10.417-7.569zM16.787 22.557l7.569 5.471-2.848-8.843 7.569-5.471h-9.368l-2.848-8.843-2.848 8.843h-9.368l7.569 5.471-2.848 8.843z" fill="#4ae0e1"></path></g><!----><!----></svg><!----></div>

动作动作=新动作(驱动程序);WebElement review_stars=driver.findElement(By.xpath("// [name()='svg' and contains(@class,'rvs-star-svg')]/ [name()='path']")) ; action.mo veToElement(review_stars).click().build().perform();

4

2 回答 2

2

请先试试这个:

driver.findElement(By.xpath("(//review-star[@class='rvs-svg']//*[name()='svg'])[4]")).click();

或者

WebElement review_stars = driver.findElement(By.xpath("(//review-star[@class='rvs-svg']//*[name()='svg'])[4]"));
action.moveToElement(review_stars).build().perform();
review_stars.click();
于 2021-08-12T14:54:22.080 回答
1

将鼠标悬停在每颗星上,然后最后单击第四颗星,您可以使用以下代码。

示例代码:

Actions action = new Actions(driver);
List<WebElement> all_stars = driver.findElements(By.xpath("//div[@class='rating-box-wrapper']//*[local-name()='svg']"));
for (int i = 0; i<5; i++) {
    Thread.sleep(2000);
    action.moveToElement(all_stars.get(i)).build().perform();
    if(i == 3) {
        action.moveToElement(all_stars.get(i)).click().build().perform();
    }
}
于 2021-08-13T02:01:35.973 回答