English 中文(简体)
设置一个实时超时以装入Sellenium WebDriver 页面吗?
原标题:Set up a real timeout for loading page in Selenium WebDriver?

我用许多代理物测试一个站点,问题在于其中一些代理物非常慢。 因此,我的代码时常被卡在加载页上。

from selenium import webdriver

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

我尝试过很多东西,比如 explicate waits impically waits intimpically waits , 并一直在四处搜寻相当长一段时间, 但仍未找到解决办法或工作变通。 似乎没有什么能真正影响页面加载行 browser. get ("http://example.com/example-page.php") (http://example. com/example-page.php") , 这也是为什么它总是卡在那里的原因。

有人有办法解决吗?

<强 > 更新1号:

JimEvans的回答解决了我的前一个问题, 并且这里 您可以找到这个新特性的 Python补丁 。

<强 > 新问题:

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 (>) 意指如果该页面加载超过30秒,它就会丢出一个超时的例外,但问题是它没有力量在像 elements. kick () 这样的页面加载页面上载之前再加载,尽管它不会阻挡到新页面全部加载之前, another_ement=浏览器.find_element_by_id (“another_ementlement”_by_id) 是其中的新疼痛,因为要么是 excol clodicly waits waterwaits wait> waits 会等待整个页面来加载上该元素。 在一些极端情况下,这将甚至需要 < geger>HOURES 。

最佳回答

您可以尝试使用图书馆中引入的页面加载超时。 其实施并不普遍, 但被. NET 和 Java 的绑定部分暴露, 并且已经在 Firefox 驱动器中实施, 并在即将到来的 2. 22 的 IE 驱动器中实施。 在 Java, 将页面加载超时设定为 15 秒, 设定密码将看起来是这样 :

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

如果它没有在 Python 语言绑定中曝光, 我相信维护者会热切地接受一个执行它的补丁 。

问题回答

您仍然可以加快脚本执行, 等待5-8秒的预期元素出现( 而不是等待可见度), 然后发送窗口. stop () JS Script (停止加载更多元素) 而不等待整页加载或超时( 超过5-8秒), 然后拨打窗口 。 stop ()

因为如果页面没有采用懒惰的装载技术( 只装载可见的元素, 只装入滚动后的元素其余部分), 它会在返回窗口前装入每个元素。 已经准备状态, 因此如果元素中的任何元素需要更长的时间, 则速度会更慢 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签