Selenium 可以检测 Python3 中网页何时完成加载吗?
在 python 中我写道:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get(url)
try:
WebDriverWait(driver, 10).until(
lambda driver: driver.execute_script('return document.readyState') == 'complete')
except se.TimeoutException:
return False
# Start Parsing
尽管我在解析某些网站时已经等待了readyState,但我发现没有复选框。但是,如果我在解析同一网站之前添加 time.sleep(5)
,我会发现有一个复选框。
我的问题是,如何才能拥有适用于大多数网站的通用解决方案?我不能只写 time.sleep(5)
因为有些网站可能需要更多,有些可能会在 0.001 秒内完成(这会对性能产生不良影响......)
我只是想刺激一个真正的浏览器,并且在刷新按钮再次出现之前不处理任何内容(这意味着所有内容都已加载)。
In python I wrote:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get(url)
try:
WebDriverWait(driver, 10).until(
lambda driver: driver.execute_script('return document.readyState') == 'complete')
except se.TimeoutException:
return False
# Start Parsing
Even though I have waited for readyState
for some websites when I parse it I see that there is no checkbox. But, If I add time.sleep(5)
Before parsing for the same website I get that there is a checkbox.
My question is, how can I have a general solution that works with the majority of websites? I can't just write time.sleep(5)
as some websites might need much more and some might finish within 0.001 seconds (which will have bad impact on performance...)
I just want to stimulate a real browser and not to handle anything before the refresh button appears again (which means everything was loaded).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理想情况下,通过
get()
访问 Web 应用程序时,仅在document.readyState
等于complete
。因此,除非 AUT(被测应用程序)表现不佳,否则以下代码行通常是一种开销:但是,根据您的测试要求,您可以配置 pageLoadStrategy 可以是:
无
渴望
正常
。需要注意的是,使用
time.sleep(secs)
没有任何特定条件即可实现达不到目的自动化,应该不惜一切代价避免。解决
方案 适用于所有网站的通用方法是根据流行的测试场景引发 WebDriverWait。举个例子:
presence_of_element_ located()
的预期条件element_to_be_clickable()
Ideally web applications when accessed through
get()
, returns the control to the WebDriver only whendocument.readyState
equals tocomplete
. So unless the AUT(Application under Test) behaves otherwise, the following line of code is typically an overhead:However, as per your test requirements you can configure the pageLoadStrategy either as:
none
eager
normal
At this point, it is to be noted that using
time.sleep(secs)
without any specific condition to achieve defeats the purpose of Automation and should be avoided at any cost.Solution
The generic approach that would work with all the websites is to induce WebDriverWait as per the prevailing test scenario. As an example:
presence_of_element_located()
visibility_of_element_located()
element_to_be_clickable()