等待页面中的“加载”图标消失

5
我们正在为 Web 应用程序进行自动化,大多数情况下,加载图标将出现在页面中心..我们需要等待该图标消失。
<div id="loading" style="display: none; visibility: hidden;">
<div></div>
<div></div> 

例如:我们在大多数情况下都有搜索功能,但是经常会出现这个加载图标。

我们使用selenium webdriver:我们正在获得的加载完成的id是"id =“loading”"。请提供解决上述问题的解决方案。

我们有不同的功能,如单击和发送键。

4个回答

5

显式等待应该会有帮助:

public static String waitForElementNotVisible(int timeOutInSeconds, WebDriver driver, String elementXPath) {
    if ((driver == null) || (elementXPath == null) || elementXPath.isEmpty()) {

        return "Wrong usage of WaitforElementNotVisible()";
    }
    try {
        (new WebDriverWait(driver, timeOutInSeconds)).until(ExpectedConditions.invisibilityOfElementLocated(By
                .xpath(elementXPath)));
        return null;
    } catch (TimeoutException e) {
        return "Build your own errormessage...";
    }
}

1
我还使用了显式等待条件来等待加载图标消失。但有时仍会遇到问题。我注意到的一件事是在加载符号显示和单击任何搜索图标之间存在一些毫秒间隔。如果我们的代码在此期间执行,则会再次遇到问题。 - SanjayDVG

2

我最近遇到了这个问题。我的解决方案可能听起来有些“hacky”,但在我的情况下效果很好:

public static void loadingWait(WebDriver driver, WebElement loader) {
    WebDriverWait wait = new WebDriverWait(driver, 5000L);
    wait.until(ExpectedConditions.visibilityOf(loader)); // wait for loader to appear
    wait.until(ExpectedConditions.invisibilityOf(loader)); // wait for loader to disappear
}

在单击提交按钮后,您可以调用此方法。您必须将驱动程序和加载器 Web 元素传递给此方法。


1
如果这个元素只在进程时间过长时出现,会怎么样呢? - White_King

0

您也可以等待ajax调用完成。在大多数情况下,当所有ajax调用完成时,加载轮会消失:

WebDriverWait wait = new WebDriverWait(d, timeOutSeconds);
wait.until(waitForAjaxCalls()); 

public static ExpectedCondition<Boolean> waitForAjaxCalls() {
        return new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                return Boolean.valueOf(((JavascriptExecutor) driver).executeScript("return (window.angular !== undefined) && (angular.element(document).injector() !== undefined) && (angular.element(document).injector().get('$http').pendingRequests.length === 0)").toString());
            }
        };
    }

3
以上代码仅适用于网站使用Angular的情况。 - Gideon Mulder

0
我遇到了这个问题,解决方法非常简单:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("path_of_loader")));
通常,在您的网站上必须有像这样的东西:Image 所以使用 path_of_loader = //div[@id='circular3dG']... 您还可以尝试使用关键字搜索加载器、旋转器或使用页面检查需要长时间加载页面的加载器/旋转器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接