Tomek
Tomek

Reputation: 741

Does finding element confirm page being fully loaded?

In reference to answer: https://stackoverflow.com/a/7811812/3146582

Does waiting for random page element being found by

_wait.Until(d => d.FindElement(By.Id("Id_Your_UIElement"));

really confirms that page was already fully loaded?

As fully loaded I assume:

Upvotes: 1

Views: 971

Answers (5)

Guy
Guy

Reputation: 50949

Selenium blocks the code until it receive page load notification from the DOM. You can limit the time by using pageLoadTimeout.

However that doesn't mean the elements are visible, only exist. To make sure they are displayed you should use explicit wait with expected conditions. For example

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("id")));

This will wait for your element to be visible, i.e. the element is displayed and has a height and width that is greater than 0.

It also doesn't mean the page can't change anymore. JavaScript may start to run only after the page is fully loaded and change the page.

For more information look here.

Upvotes: 0

JimEvans
JimEvans

Reputation: 27496

No, it does not necessarily mean that the page is "fully loaded." All a successful call to findElement tells you is that element is loaded in the DOM. If you choose the element correctly, it may imply the page is fully loaded as defined by your criteria, but that will be entirely dependent on the page structure. For example, I could envision a page that fires off an XmlHttpRequest in JavaScript that's executed via setTimeout. The findElement call might succeed, but the page could still be downloading resources via that mechanism. This is one reason why there is no one-size-fits-all definition of "the page is fully loaded" in today's web. It depends on the page in question.

Upvotes: 2

Leon Barkan
Leon Barkan

Reputation: 2703

Option 1:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(120));

Option 2:

WebDriverWait wait;
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

Upvotes: 1

Buaban
Buaban

Reputation: 5137

It's quite impossible to define a generic fully loaded state for all web applications because of lazy-loading component. There are a couple practices that I use to define the loaded state. 1. Repeat counting web elements on the page until the number is stable. 2. Wait for the slowest component on the page.

Upvotes: 0

Maertin
Maertin

Reputation: 384

Use pageLoadTimeout to determine if page has been fully downloaded. Identify what's the page load expected delay from your specifications and use that time as your timeout while using this method.

Upvotes: 0

Related Questions