implicit wait in selenium

76 views Asked by At

I am very confused about how to use implicit wait in selenium

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);

I saw someone wrote those two line codes together very often. Why do we need the pageLoadTimeOut line? I think the first line is enough.

I searched a lot through google, and still confused

2

There are 2 answers

0
Guy On

Have a look at WebDriver.Timeouts

pageLoadTimeout(Duration duration)

Sets the amount of time to wait for a page load to complete before throwing an error.

implicitlyWait(Duration duration)

Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.

There is no reason to write those in several places, this is one time setting that remains during the driver life time.

You should also note that you are using deprecated methods implicitlyWait(long time, TimeUnit unit) and pageLoadTimeout(long time, TimeUnit unit).

1
mahesh tawde On

The purpose of two timeouts is completely different hence both these are called as part of setup in frameworks/scripts.

implicitlyWait - Will be used during findElement, findElements call to wait for the timeout duration before returning NoSuchElementException, Empty List respectively.

pageLoadTimeout - Is used by Selenium implicitly whenever a page is getting loaded and it reads the document.readyState property of your web page.However this would only work for synchronous page loads and not for AJAX calls or dynamic content getting loaded on web page.

Since both these use cases are completely different, we use different wait strategies. Since a page load might take longer than loading a dynamic element on web page.