Tuesday 23 August 2016

Selenium: WebDriver: Waiting for webpage to load

While doing automation on Web Applications, you may face this kind of problem, where you are trying to find an element, but page is not loaded fully, you may endup in NoSuchFrameException. Pagy may load slowly due to network issues (or) application issues, to handle these kind of situations, we need to wait until the page loaded fully (or) at least the web element we are looking for is loaded.

WebDriver.Timeouts interface provides following methods to set the time outs for complete page load, you can specify amount of time the driver should wait when searching for an element if it is not immediately present, You can specify the amount of time to wait for an asynchronous script to finish execution before throwing an error. Following table summarizes the methods defined in WebDriver.Timeouts interface.

Method
Description
Timeouts implicitlyWait(long time, TimeUnit unit)
Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException.
Timeouts setScriptTimeout(long time, TimeUnit unit)
Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error.
Timeouts pageLoadTimeout(long time, TimeUnit unit)
Sets the amount of time to wait for a page load to complete before throwing an error.

Example
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


In my next post, I am going to explain about explicit time outs usage.



Previous                                                 Next                                                 Home

No comments:

Post a Comment