当前位置: 首页 > 知识库问答 >
问题:

将鼠标悬停在每颗星星上,然后最后单击第四颗星

暨修洁
2023-03-14

在一个网页上,我有一个评分星反馈,当我悬停在星星上时,它们变成了黄色。我有5/6类似的div,相同的类和配置。如果我吸气并单击第4颗星,它们都变成了黄色。我想做的是瞄准第4颗星并单击它来设置评级。我尝试了动作链

<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=驱动程序。findElement(通过.xpath(“///[name()='svg'和contains(@class,'rvs-star-svg'))]/[name()='path']”));行动mo veToElement(review_stars)。单击()。build()。执行();

共有2个答案

汝彭薄
2023-03-14

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

示例代码:

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();
    }
}
华阳秋
2023-03-14

请先试试这个:

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();
 类似资料:
  • 我使用Selenium使用Java语言自动化UI。 我有一个动作按钮,当用户将鼠标悬停在动作按钮上时,它会给出两个可点击的选项——创建和编辑 我已经将CSS定位符存储为动作按钮的枚举,以及创建和编辑(可点击的)链接的枚举,分别为Action button、CREATEACTION、EDITACTION。 我使用了如下Java代码,但它给出了一个错误java.lang.ClassCastExcept

  • 我正在尝试将鼠标悬停在动画菜单上并在菜单中选择一个项目。我尝试先通过xpath在菜单上执行悬停,然后通过xpath执行单击菜单项,如下所示。 我有个例外 org.openqa.selenium.interactions。MoveTargetOutOfBoundsException:元素内的偏移量无法滚动到视图中 我还尝试将这些行为链接如下: 这也会引发相同的异常。 有什么想法来实现动画菜单项的点击

  • 笔试题: 随机获取10到999之间的100个整数,并且要从大到小排序,要求使用快速排序算法。         获取1到99之间的10个整数         参数设计: min最小值,max最大值,total多少个 <script> function getRandomNum(min,max,total){ let arr=[] for

  • 我使用石英调度和Spring Batch,我需要在每个月的最后一个星期四运行一个特定的工作。 有可能创建这样的Quartz cron表达式吗? 谢谢

  • 问题内容: 我有一张图片: 然后,我将其显示在屏幕上: 如何检测鼠标是否在触摸图像? 问题答案: 使用获得描述你的边界,然后使用来检查,如果鼠标光标这里面。 例:

  • 我是一个用cucumber和水豚配食的新手。我需要在使用capybara将鼠标悬停在网页的某些元素上后单击显示的链接 例如att.com 1、场景悬停在个人点击att.com上 悬停在商店上方的另一个场景——捆绑包——点击热门捆绑包 如何使用水豚的悬停和点击方法实现这一点,或者是否有其他方法可以实现这一点。 尝试的选项是 但它抱怨找不到xpath