繁体   English   中英

Webdriver 没有正确计算页面上的元素(JAVA SEELENIUM)

[英]Webdriver doesn't counts the elements on page properly (JAVA SELENIUM)

我正在尝试计算页面上的元素。 它工作了一整年都很好,但现在我的方法总是给出错误的值,所以我的断言失败了。 它给出没有任何逻辑关系的随机数。

public int getCountOfResults (Webdriver webdriver) {
    try {
         waitUntilFoundByLocatorAndIsDisplayed(webDriver,By.xpath("//div[@class='hello']/div/a"));
         List <WebElement> countProduct = webDriver.findElements(By.xpath("//div[@class='hello']/div/a"));
         return countProduct.size();
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

看来你很接近了。 但是,我们没有方法waitUntilFoundByLocatorAndIsDisplayed()的可见性。 作为解决方案,您可以为visibilityOfAllElementsLocatedBy()引入WebDriverWait ,您可以使用以下解决方案:

public int getCountOfResults (Webdriver webdriver) {
    try {
        waitUntilFoundByLocatorAndIsDisplayed(webDriver,By.xpath("//div[@class='hello']/div/a"));
        List <WebElement> countProduct = new WebDriverWait(webDriver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='hello']/div/a")));
        return countProduct.size();
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM