我有使用隐式等待预配置的方法,该方法配置为向我返回一些WebElement
:
private WebElement findElement(By locator) {
WebElement element = null;
for (int i = 0; i <= numberOfTries && element == null; i++)
try {
waiter.until(ExpectedConditions.elementToBeClickable(locator));
element = driver.findElement(locator);
} catch (TimeoutException | NoSuchElementException e) {
System.out.println(specificStringAboutTimeOutAndNoSuchEl);
} catch (WebDriverException e) {
Throwable cause = null;
Throwable result = e;
while (null != (cause = result.getCause()) && (result != cause))
result = cause;
System.out.println(specificStringAboutException);
System.out.println(result);
}
return element;
}
这等到其他元素消失,例如微调器和加载屏幕:
private void waitsElementToVanish(By locator, String previousOp) {
List<Class<? extends Throwable>> ignorableExceptions = new ArrayList<>();
ignorableExceptions.add(StaleElementReferenceException.class);
ignorableExceptions.add(NoSuchElementException.class);
try {
if (driver.findElements(locator).size() > 0)
System.out.println(locator.toString() + " exists");
waiter.ignoreAll(ignorableExceptions).until(ExpectedConditions.visibilityOfElementLocated(locator));
waiter.ignoreAll(ignorableExceptions).until(ExpectedConditions.invisibilityOfElementLocated(locator));
} catch (TimeoutException e) {
System.out.println("timeout waiting " + locator.toString() + " to vanish");
System.out.println(previousOp);
}
}
我这样使用它们:
waitsElementToVanish(By.className("spinnerLoading"), "someString");
findElement(By.xpath(otherElement)).click();
我在.click()
收到此错误:
org.openqa.selenium.WebDriverException: unknown error: Element <div class="menuContent ellipsis flex flexGrow">...</div> is not clickable at point (136, 240). Other element would receive the click: <div class="spinnerLoading">...</div>
This是我正在使用的网站,该应用程序纯粹是教育性的。这是the entire code。
我应该将Selenium
click
方法包装到某些WebDriverException中,例如:
spinnerClass
时抛出WebDriverException,请尝试单击,然后转到waitsElementToVanish
,然后重试ReallyMessedUpExceptionDoSomething!
异常。我的问题是:我想不惜一切代价避免使用Thread.sleep()
。我想优化测试时间,仅等待即将发生的事情,而不是在没有检查页面上任何内容的预定时间段内等待。 “良好实践操作”真的是许多尝试的总结吗?即使在之前的生产线也尝试过这种方法时,我如何才能确保避免纺纱呢?而且我没有发现任何包装环绕循环的问题,可以尝试一次又一次的限制。但是,实际上,最好的解决方案是什么?