How to properly configure Implicit / Explicit Waits and pageLoadTimeout through Selenium?

implicitlyWait()

implicitlyWait() is to tell the WebDriver instance i.e. driver to poll the HTML DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default wait configuration is set to 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Your code trial is just perfect as in:

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Here you will find a detailed discussion in Using implicit wait in selenium


pageLoadTimeout()

pageLoadTimeout() sets the timespan to wait for a page load to be completed before throwing an error.

Your code trial is just perfect as in:

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

Here you can find a detailed discussion in pageLoadTimeout in Selenium not working

Note : Try to avoid configuring pageLoadTimeout() until and unless the Test Specification explicitly mentions about the same.


Why WebDriverWait?

Modern browsers uses JavaScript, AJAX and React Native where elements within an webpage are loaded dynamically. So to wait for a specific condition to be met before proceeding for the next line of code Explicit Waits i.e. WebDriverWait is the way to proceed ahead.

Note : As per the official documentation of Explicit and Implicit Waits Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.

Your code trial is just perfect to wait for the visibility of an element as in:

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));

Here you can find a detailed discussion of Replace implicit wait with explicit wait (selenium webdriver & java)


Your specific questions

  • Why is it necessary to assign a WebElement to the following wait : WebDriverWait in conjunction with ExpectedConditions not only returns a WebElement but depending on the ExpectedConditions can return void, Boolean, List too.

  • What does WebElement element receive? : As per your code block where you have used ExpectedConditions as visibilityOfElementLocated(), the WebElement will be returned once the element is present on the DOM Tree of the webpage and is visible. Visibility means that the elements are not only displayed but also has a height and width that is greater than 0.

  • Is this the right implementation? : Your implementation was near perfect but the last line of code i.e. boolean status = element.isDisplayed(); is redundant as visibilityOfElementLocated() returns the element once the element is visible (i.e. the elements are not only displayed but also has a height and width that is greater than 0).

Leave a Comment