Selenium 可以检测 Python3 中网页何时完成加载吗?

发布于 2025-01-16 21:40:14 字数 633 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酒忠诚 2025-01-23 21:40:14

理想情况下,通过 get() 访问 Web 应用程序时,仅在 document.readyState 等于complete。因此,除非 AUT(被测应用程序)表现不佳,否则以下代码行通常是一种开销:

WebDriverWait(driver, 10).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')

但是,根据您的测试要求,您可以配置 pageLoadStrategy 可以是:

  • 渴望
  • 正常

您可以在 在 Selenium Python 中检查网站 .readyState 的正确语法是什么<中找到详细讨论/p>

。需要注意的是,使用 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 when document.readyState equals to complete. So unless the AUT(Application under Test) behaves otherwise, the following line of code is typically an overhead:

WebDriverWait(driver, 10).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')

However, as per your test requirements you can configure the pageLoadStrategy either as:

  • none
  • eager
  • normal

You can find a detailed discussion in What is the correct syntax checking the .readyState of a website in Selenium Python

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:

  • To wait for the presence of an element you need to invoke the expected_conditions of presence_of_element_located()
  • To wait for the visibility of an element you need to invoke the expected_conditions of visibility_of_element_located()
  • To wait for the element to be visible, enabled and interactable such that you can click it you need to invoke the expected_conditions of element_to_be_clickable()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文