python selenium 等待页面加载完毕,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....

解决方案

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.

An Alternate Approach

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"))

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

Why No

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.

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.

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)

References

You can find a couple of relevant discussions in:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值