如何在 selenium (Python) 中测量网站的页面下载和呈现时间?目前我有类似的东西
... # 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
但它不太有效,因为 get() 不能保证在页面呈现完成之前阻塞。它的official doc还说
In some circumstances, WebDriver may return control before the page has finished, or even started, loading.
事实上,在我的简短测试代码中,打印语句实际上可以在 webdriver 完成加载之前进一步打印两三个 url。
我知道在某些网页元素上使用显式等待可以强制阻止,但是有没有一种通用的方法可以做到这一点?我在大约几千个网站上对此进行了测量,因此拥有独立于网络内容的内容会很棒。
请您参考如下方法:
确保所有内容都已加载的唯一方法是使用 ExplicitWait .缺点是您必须知道哪个元素会减慢每个特定页面的呈现速度,但这是保持测量精度的唯一方法。
start = datetime.now()
browser.get(currDomainName)
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID,
"reallyHugeImageId")))
end = datetime.now()
请注意 time.sleep() 会破坏您的措施,因为您总是会等待准确的时间。因此,唯一的方法是调查哪些元素尚未呈现,即使 WebDriver 返回并通过 ExpectedCondition 等待它们。
