I have this method using implicit wait pre configured to return me some 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;
}
And this to wait to some other element vanish, like spinners and loading screens:
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);
}
}
And I use them like this:
waitsElementToVanish(By.className("spinnerLoading"), "someString");
findElement(By.xpath(otherElement)).click();
And I get this error at .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 is the website I'm using and this application is purely educational. This is the entire code.
Should I wrap Selenium
click
method into some WebDriverException like:
- Tries to click, if WebDriverException is thrown with
spinnerClass
on it, goes towaitsElementToVanish
and then tries again - If number of tries goes beyond limit, then it throws some
ReallyMessedUpExceptionDoSomething!
exception.
My question is: I want to avoid Thread.sleep()
at all costs. I want optimize test time waiting only for what it's coming, not for some pre fixed period without checking anything on page. The 'good practice actions' are really wrap click on many tries? How I can assure I'm avoiding spinner even when the line before tried that too? And I don't see any problem wrapping click on a loop to try again and again with a limit. But, actually, what is the best solution here?