首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >悬停元素列表- Selenium Java WebDriver

悬停元素列表- Selenium Java WebDriver
EN

Stack Overflow用户
提问于 2018-10-20 22:55:56
回答 3查看 1.6K关注 0票数 1

下面是我尝试使用Selenium WebDriver (2.53.1)和Java进行测试的场景。

在一个网页上,我有一个明星名单。我想要将鼠标悬停在每一个上面,当我们鼠标悬停时,星星会突出显示。然后点击其中一颗星。当每颗星在空中悬停时,css都会发生变化。

悬停之前的

代码语言:javascript
复制
  <div class="wh-rating-choices" style="display: none;">
        <div class="wh-rating-choices-holder">
                                <a href="#">1</a>
                                <a href="#">2</a>
                                <a href="#">3</a>
                                <a href="#">4</a>
                                <a href="#">5</a>
                            <em>Your Rating: <span></span></em>
        </div>
    </div>

悬停后的

代码语言:javascript
复制
   <div class="wh-rating-choices" style="display: none;">
        <div class="wh-rating-choices-holder">
                                <a href="#" class="hover">1</a>
                                <a href="#" class="hover">2</a>
                                <a href="#" class="hover">3</a>
                                <a href="#" class="hover">4</a>
                                <a href="#" class="hover">5</a>
                            <em>Your Rating: <span>Excellent</span></em>
        </div>
    </div>

所以基本上,在成功悬停时,类' hover‘会被添加到html/css中。

我尝试过的代码如下所示。

代码语言:javascript
复制
List<WebElement> allStars = driver.findElements(By.xpath("//a[@class='hover']"));
System.out.println("<<<<<<<<<<<<------List of all stars, size------------>>>>>>>>>>"+allStars.size());
for (WebElement e : allStars) {
    Actions act = new Actions(driver);
    act.moveToElement(e).build().perform();
    Thread.sleep(5000);
}

和之前一样,没有添加类' hover‘,WebElements的列表始终为零。在一些selenium站点上尝试了一些建议的选项,但不起作用。请帮助,如何在这一点上进行。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-10-22 16:44:17

我刚刚测试了一个解决方案,但它非常粗糙。然而,它是有效的。

注意:直接导航到第五颗星(文本为“5”的元素)对我不起作用。似乎您需要悬停,以便评级持有者框打开,然后悬停到第五颗星,以便您获得所有的class=“悬停”。

这是我所做的:

--使用操作导航到上面的元素(“编写评论”)

--以1像素为增量向下移动(正"y")

--每次递增后,测试具有"wh-rating-choices“类的元素是否包含字符串"block”

--如果是,则移动到“wh-rating-choices holder”类元素下包含文本"5“的元素。

我在python中对其进行了测试,但以下是在Java中应该可以工作的内容:

代码语言:javascript
复制
Actions action = new Actions(driver);
int inc = 0;
while (inc < 100) {
    WebElement top = driver.findElement(By.xpath("//*[contains(text(), 'Write a Review')]"));
    action.moveToElement(top, 0, inc).contextClick().perform();
    Thread.sleep(200);
    a = driver.findElement(By.xpath("//*[contains(@class, 'wh-rating-choices')]"));
    if (a.getAttribute("style").contains("block") {
        aa = driver.findElement(By.xpath("//*[contains(@class, 'wh-rating-choices-holder')]"));
        bb = aa.findElement(By.xpath(".//*[contains(text(), '5')]"));
        action.moveToElement(bb).perform();
        break;
    }
    inc++;
}
System.out.println(bb.getAttribute("outerHTML"));

Thread.sleep(200)可能过高了,可以尝试更低的值,比如50或20。

PS。您可能需要先关闭包含class="af-icon-cross"的弹出窗口

票数 1
EN

Stack Overflow用户

发布于 2018-10-20 23:34:45

看起来你们很亲近。您需要为带有class="hover"<a>元素引入可单击的WebDriverWait,您可以使用以下解决方案:

代码语言:javascript
复制
WebElement rating_holder = driver.findElement(By.xpath("//div[@class='wh-rating-choices']"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", rating_holder)
List<WebElement> allStars = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='wh-rating-choices']/div[@class='wh-rating-choices-holder']//a")));
System.out.println("<<<<<<<<<<<<------List of all stars, size------------>>>>>>>>>>"+allStars.size());
for (WebElement e : allStars) {
    if(e.getAttribute("innerHTML").contains("5"))
    {
        new Actions(driver).moveToElement(e).build().perform();
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='wh-rating-choices-holder']//a[@class='hover']"))).click();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-10-21 03:06:08

你的代码的问题是,你正在寻找在你悬停之前就已经有' hover‘类的A标记。正如你所说的,' hover‘类直到悬停发生后才会被添加。因此,您需要更改初始定位器,使其不包含“hover”类。

我更喜欢使用CSS选择器而不是XPath,除非需要XPath (通过包含的文本或DOM遍历查找元素)。你可以在谷歌上搜索更多信息。这是经过测试的代码。

代码语言:javascript
复制
// find all A tags inside the containing DIV
List<WebElement> stars = driver.findElements(By.cssSelector("div.wh-rating-choices-holder > a"));

// loop through each element and hover
Actions action = new Actions(driver);
for (WebElement e : stars)
{
    action.moveToElement(e).perform();
}

// after all the hovering is done, fetch the same elements but expect that they will now contain the 'hover' class
stars = driver.findElements(By.cssSelector("div.wh-rating-choices-holder > a.hover"));

// Assert (TestNG) that there are 5 stars that were hovered
Assert.assertEquals(stars.size(), 5, "Verify 5 elements were hovered");

// click the 5th star
stars.get(4).click();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52906949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档