最近将我的操作系统更改为 Debian Wheezy(KDE) 我的网络驱动程序后, 行为奇怪。 (在 Firefox 中使用 python 2.7 进行编码,所有模块都是最新的。)
它完全忽略等待命令,例如implicitly_wait()。 如果迫切需要等待才能访问,这是一个很大的问题 加载网页元素。
一个肮脏的解决方法是使用 time.sleep(),它是有效的,但是 当然不是 webdriver 的使用方式。
以下代码举例说明了我的问题:
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get('http://www.google.com')
tc = time.clock()
tw = time.time()
driver.implicitly_wait(60) # should halt here for 60s
print "CPU time: ", time.clock() - tc
print "Wall time: ", time.time() - tw
driver.quit()
脚本运行结束时没有任何错误消息,但是 等待语句被完全忽略。
输出:
CPU time: 0.0
Wall time: 1.1845741272
我对这种神秘的行为没有任何解释或任何起点 寻找答案。
因此,我热烈欢迎任何建议或解决方案。 谢谢!
最佳答案
不,implicitly_wait()
不会在您调用它时停止/停止执行。每个 session 调用一次,并设置 selenium 查找元素或执行命令时使用的隐式超时,引用自 documentation :
implicitly_wait(time_to_wait)
Sets a sticky timeout to implicitly wait for an element to be found, or a command to complete. This method only needs to be called one time per session. To set the timeout for calls to execute_async_script, see set_script_timeout.
我同意方法名称有点令人困惑,set_implicit_timeout()
可能是更好的选择。
另请参阅:
关于python - Webdriver 忽略等待 - Firefox、Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27135933/