简体   繁体   English

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

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

I'm trying to count the elements on the page.我正在尝试计算页面上的元素。 It worked for almsot a whole year perfectly fine but now my method is always giving a wrong value so my assertion fails.它工作了一整年都很好,但现在我的方法总是给出错误的值,所以我的断言失败了。 It gives random numbers that dont have any logical relationship.它给出没有任何逻辑关系的随机数。

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;
    }
}

Seems you were close.看来你很接近了。 However we don't have the visibility to the method waitUntilFoundByLocatorAndIsDisplayed() .但是,我们没有方法waitUntilFoundByLocatorAndIsDisplayed()的可见性。 As a solution you can induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use the following solution:作为解决方案,您可以为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