本文介绍了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?

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

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.

默认情况下,通过.因此,重写相同内容就是完整的开销.除非'document.readyState'等于 "complete" ,否则客户端(即Web浏览器)将永远不会将控件返回到WebDriver实例.一旦满足此条件,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.

值得一提的是,尽管实现'document.readyState' equal to "complete"后,客户端(即Web浏览器)可以将控件返回给WebDriver实例,但是它不能保证新 HTML DOM 存在 可见可互动 clickable .

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.

因此,根据我们的要求,如果在达到'document.readyState' equal to "complete"时与您交互的下一个* WebElement不具有 interactable ,则必须诱使 WebDriverWait 与匹配的期望条件.以下是一些最常用的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)
  • element_to_be_clickable(locator)
  • presence_of_element_located(locator)
  • visibility_of(element)

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

You can find a couple of relevant discussions in:

  • Do we have any generic function to check if page has completely loaded in Selenium
  • Selenium IE WebDriver only works while debugging
  • Selenium how to manage wait for page load?

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

07-31 12:48