簡體   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