fy_iceworld
fy_iceworld

Reputation: 666

How to measure page download time with Selenium

How do I go about measuring page download+rendering time of a website in selenium (Python)? Currently I have something like

... # set up web driver and loop through a list of domain names

print currDomainName
start = datetime.now()
browser.get(currDomainName)
end = datetime.now()

... # do something with the time diff

But it doesn't quite work because get() isn't guaranteed to block until page render is complete. Its official doc also says

In some circumstances, WebDriver may return control before the page has finished, or even started, loading.

In fact, in my short test code the print statement could literally be printing two or three urls further down before the one in webdriver finishes loading.

I know using explicit wait on certain webpage elements can enforce blocking, but is there a generic way of doing this? I'm measuring this on about a few thousand websites, so it would be great to have something independent from the web content.

Upvotes: 2

Views: 3236

Answers (1)

Petr Mensik
Petr Mensik

Reputation: 27496

Only way how to make sure that everything is loaded is to use ExplicitWait. Downside is that you have to know which element slows the rendering of each particular page but this is the only way how to keep your measure precise.

start = datetime.now()
browser.get(currDomainName)
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 
   "reallyHugeImageId")))
end = datetime.now()

Note that time.sleep() would break your measures because you would always wait exact time. So only way is to investigate which elements are not yet rendered even if the WebDriver returns and wait from them through ExpectedCondition.

Upvotes: 1

Related Questions