When you do the stated flow, when you want to click on the Logout button, you might get the above exception as the button is overlapped by the toaster message. Webdriver will not be able to click on the button and throws a perfect exception stating other elements would receive the click.
Solution:
You need to wait until the toaster message disappears. And we know how to wait in selenium.
To solve the above issue, You must use the wait provided by selenium. Now you will ask which wait we should use?
Thread.sleep(value): It is not advisable. You never know how much time you need to make web driver sleep. It might increase your test execution time drastically and would not serve the purpose always.
Implicit wait: This will not work always. The implicit wait will make the web driver wait until it does not find it. Here the issue is that webdriver finds web elements but could not click on it or element is not visible. The implicit wait can not stop webdriver once web element is found. This wait does not bother if the element is displayed or not.
Explicit wait: This is called dynamic wait and we should use it. ExpectedConditions class provides so many conditions until you want to wait. We have a condition which we can use to wait until the element is displayed. It will make web driver wait till the element is displayed on UI, not till the time element is found.
Sample code:
WebDriverWait wait= new WebDriverWait(driver,15);
wait.until(ExpectedConditions.visibilityOf(element));
Here's the code for Explicit Wait:
WebDriver driver= new ChromeDriver();
WebDriverWait wait= new WebDriverWait(driver, 30);
WebElement Element= wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("Some Expression"))));
Element.click();
Explanation:
WebDriverWait is a class. We can create an object of it by passing WebDriver instance and timeout in seconds. WebDriverWait will ignore instances of NotFoundException that are encountered (thrown) by default in the ‘until’ condition, and immediately propagate all others. You can add more to the ignore list by calling ignoring(exceptions to add)
An exception will be called based on expected condition in case of time out and condition is not satisfied. We can pass timeout only in seconds.
ExpectedConditions is a class that has canned ExpectedConditions which are generally useful within web driver tests. For example: elementToBeClickable etc.
All canned expected conditions are static methods of ExpectedConditions class. To view all canned conditions, do” ExpectectedCondition.(dot)”.
This waits up to 30 seconds before throwing a TimeoutException or if it finds the element will return it in 0 – 30 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. Successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.
Like implicit wait, the explicit wait will also not wait further if the condition is satisfied before timeout reaches.
The explicit wait is intelligent to wait as it waits dynamically for the specified conditions.
I hope it helps!
If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training!