python-selenium 有没有办法等到页面的所有元素都加载完毕? [英] Is there a way with python-selenium to wait until all elements of a page has loaded?

查看:91
本文介绍了python-selenium 有没有办法等到页面的所有元素都加载完毕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般要求检查页面的所有元素是否已加载.有没有办法检查基本上?

I am asking for generally checking if all elements of a page has been loaded. Is there a way to check that basically?

在具体的例子中有一个页面,我点击了某个按钮,然后我必须等到我点击下一步"按钮.但是,此下一步"按钮始终可用、可选择和单击.那么如何使用 selenium 检查页面的状态"(?)?

In the concrete example there is a page, I click on some button, and then I have to wait until I click on the 'next' button. However, this 'Next' button is available, selectable and clickable ALL THE TIME. So how to check with selenium that 'state'(?) of a page?

提醒一下:这是一个关于硒的问题,而不是相关网页的质量......

As a reminder: This is a question about selenium and not the quality of the webpage in question....

推荐答案

因为您的问题是关于 python-selenium 是否有办法等到页面的所有元素都加载完毕,所以答案是.

As your question is about if there is a way with python-selenium to wait until all elements of a page has loaded, the Answer is No.

基本上可以写一行代码(或者通过函数实现)来检查'document.readyState' == "complete"如下:

Fundamentally, you can write a line of code (or implement it through a function) to check for the 'document.readyState' == "complete" as follows :

self.driver.execute_script("return document.readyState").equals("complete"))

但是这种方法有一个缺点,它没有考虑到 JavaScript/AJAX 调用 的完成.

But this approach have a drawback that it doesn't accounts for the JavaScript / AJAX Calls to be completed.

编写上述行以等待页面加载默认通过.所以重写相同的内容是一个完整的开销.客户端(即 Web 浏览器)永远不会将控制权返回给 WebDriver 实例,除非 'document.readyState' 等于 "complete".一旦满足这个条件,Selenium 就会执行下一行代码.

Writing the above mentioned line to wait for Page Loading is implemented by default through Selenium. So rewriting the same is a complete overhead. The client (i.e. the Web Browser) will never return the control back to the WebDriver instance until and unless 'document.readyState' is equal to "complete". Once this condition is fulfilled Selenium performs the next line of code.

值得一提的是,虽然客户端(即 Web 浏览器)可以在实现 'document.readyState' 等于完成" 后将控制权返回给 WebDriver 实例,但它不会't 保证新 HTML DOM 上的所有 WebElements 是否存在可见可交互可点击.

It's worth to mention that though the client (i.e. the Web Browser) can return back the control to the WebDriver instance once 'document.readyState' equal to "complete" is achieved, it doesn't guarantees whether all the WebElements on the new HTML DOM are present, visible, interactable and clickable.

因此,根据我们的要求,如果您必须与之交互的下一个 *WebElement 在 'document.readyState' 等于完成"时可交互> 实现了,你必须诱导 WebDriverWait 与匹配的 expected_conditions.以下是一些最常用的 expected_condition:

So, as per our requirement, if the next *WebElement with which you have to interact is not interactable by the time 'document.readyState' equal to "complete" is achieved, you have to induce WebDriverWait inconjunction with a matching expected_conditions with respect to the individual WebElement. Here are a few of the most used expected_condition:

  • element_to_be_clickable(locator)
  • presence_of_element_located(locator)
  • visibility_of(element)

您可以在以下位置找到一些相关讨论:

You can find a couple of relevant discussions in:

这篇关于python-selenium 有没有办法等到页面的所有元素都加载完毕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆