为Selenium WebDriver中的页面加载设置实时超时吗? [英] Set up a real timeout for loading page in Selenium WebDriver?

查看:710
本文介绍了为Selenium WebDriver中的页面加载设置实时超时吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个具有很多代理的站点,问题是其中一些代理的运行速度非常慢.因此,我的代码总是不时加载页面.

I'm testing a site with lots of proxies, and the problem is some of those proxies are awfully slow. Therefore my code is stuck at loading pages every now and then.

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("http://example.com/example-page.php")
element = browser.find_element_by_id("someElement")

我已经尝试过很多类似explicit waitsimplicit waits的内容,并且已经搜索了很长时间,但仍然没有找到解决方案或解决方法.似乎没有什么真正影响页面加载行browser.get("http://example.com/example-page.php")的原因,这就是为什么它总是卡在这里.

I've tried lots of stuff like explicit waits or implicit waits and been searching around for quite a while but still not yet found a solution or workaround. Nothing seems to really affect page loading line browser.get("http://example.com/example-page.php"), and that's why it's always stuck there.

有人对此有解决方案吗?

Anybody got a solution for this?

更新1:

JimEvans的答案解决了我之前的问题,并且

JimEvans' answer solved my previous problem, and here you can find python patch for this new feature.

新问题:

browser = webdriver.Firefox()
browser.set_page_load_timeout(30)

browser.get("http://example.com/example-page.php")

element = browser.find_element_by_id("elementA")
element.click() ## assume it's a link to a new page http://example.com/another-example.php

another_element = browser.find_element_by_id("another_element")

如您所见,browser.set_page_load_timeout(30)仅影响browser.get("http://example.com/example-page.php"),这意味着如果该页面加载超过30秒,它将抛出超时异常,但问题是它没有诸如element.click()之类的页面加载能力,尽管直到新页面完全加载后它才阻塞,但是another_element = browser.find_element_by_id("another_element")是新的难题,因为explicit waitsimplicit waits都将等待整个页面加载后才开始寻找该页面.元素.在某些极端情况下,这甚至需要 HOURS .我该怎么办?

As you can see browser.set_page_load_timeout(30) only affects browser.get("http://example.com/example-page.php") which means if this page loads for over 30 seconds it will throw out a timeout exception, but the problem is that it has no power over page loading such as element.click(), although it does not block till the new page entirely loads up, another_element = browser.find_element_by_id("another_element") is the new pain in the ass, because either explicit waits or implicit waits would wait for the whole page to load up before it starts to look for that element. In some extreme cases this would take even HOURS. What can I do about it?

推荐答案

您可以尝试使用库中引入的页面加载超时.它的实现不是通用的,但是.NET和Java绑定肯定会公开它的实现,并且已经在Firefox驱动程序和IE驱动程序的即将发布的2.22中实现了.在Java中,要将页面加载超时设置为15秒,设置该代码的代码应如下所示:

You could try using the page load timeout introduced in the library. The implementation of it is not universal, but it's exposed for certain by the .NET and Java bindings, and has been implemented in and the Firefox driver now, and in the IE driver in the forthcoming 2.22. In Java, to set the page load timeout to 15 seconds, the code to set it would look like this:

driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);

如果它没有出现在Python语言绑定中,我相信维护者会急切地接受实现它的补丁.

If it's not exposed in the Python language bindings, I'm sure the maintainer would eagerly accept a patch that implemented it.

这篇关于为Selenium WebDriver中的页面加载设置实时超时吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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