How to use waits in selenium for page to load

How to use waits in selenium for page to load

Selenium wait is a way to pause the execution of your script until a certain condition is met. This can be useful if you need to wait for an element to appear on the page or for a page to fully load before interacting with it. There are three types of waits in Selenium: implicit, explicit, and fluent.

Selenium webdriver wait using pageLoadTimeout :

This is the maximum time selenium waits for a page to load successfully on a browser. If the page takes more than this time, it will throw a Timeout Exception.

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

In the above code, pageLoadTimeout() method is accepting two arguments, one is waiting time and in another, we are specifying the Time Unit.

Here the selenium WebDriver instance will wait a maximum of 90 seconds for a web page to load. If it is loaded before the specified wait time, the execution will move to the next line of the script. If it doesn’t get loaded in 90 seconds it will throw the Timeout Exception.

Selenium webdriver wait using implicit Waits:

Implicit waits are the easiest to use and the most common type of wait. When you set an implicit wait, it will tell Selenium to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting for implicit wait is 0 seconds, which means that Selenium will not wait at all before throwing an exception if it cannot find the element. 

To set an implicit wait in selenium , you can use the driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS) method.

Selenium webdriver wait using explicit waits: 

Explicit Waits: Explicit waits are more flexible than implicit waits because they allow you to specify the exact condition that you want Selenium to wait for. For example, you can use an explicit wait to wait for an element to become clickable or to wait for an element's text to change.

To use an explicit wait in selenium , you will need to use the WebDriverWait class and the ExpectedConditions class.

Here is an example of how to use an explicit wait in Selenium:

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someElementId")));

This will wait for up to 10 seconds for the element with the ID "someElementId" to become clickable before proceeding with the script. If the element does not become clickable within 10 seconds, a TimeoutException will be thrown.

Selenium webdriver wait using fluent waits:

Fluent wait in selenium are similar to explicit waits, but they allow you to specify the frequency at which Selenium should check the condition and the maximum amount of time that the script should wait. Fluent waits are useful if you are dealing with elements that may take a variable amount of time to load. To use a fluent wait, you will need to use the FluentWait class.

Back to blog