我们正在为 Web 应用程序进行自动化,大多数场景都有加载图标出现在页面的中心。我们需要等待这个加载图标消失。
HTML:
<div id="loading" style="display: none; visibility: hidden;">
<div></div>
<div></div>
示例:我们有搜索功能,在大多数情况下我们都会收到此加载图标。
对于 Selenium Webdriver,我们使用 ID。
我们正在获取加载完成id=“loading”。请为我面临的上述问题提供任何解决方案。
我们有多种后续测试功能,例如点击和发送键,只有在加载完成后才能使用。
显式等待应该有帮助:
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...";
}
}
我最近遇到了这个问题。我的解决方案可能听起来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 元素传递给此方法。
您也可以等到 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());
}
};
}
我遇到了这个问题,解决方案非常简单:
WebDriverWait 等待 = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until( ExpectedConditions.invisibilityOfElementLocated(By.xpath("path_of_loader")));
所以使用 path_of_loader = //div[@id='circular3dG'] .. 您还可以尝试使用 loader、spinner 等关键字进行搜索,或者使用页面来检查 loader/spinner 加载页面需要很长时间的地方。