Python 让 Selenium 等待 10 秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45347675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make Selenium wait 10 seconds
提问by hansTheFranz
Yes I know the question has been asked quite often but I still don't get it. I want to make Selenium wait, no matter what. I tried these methods
是的,我知道这个问题经常被问到,但我仍然不明白。我想让 Selenium 等待,无论如何。我试过这些方法
driver.set_page_load_timeout(30)
driver.implicitly_wait(90)
WebDriverWait(driver, 10)
driver.set_script_timeout(30)
and other things but it does not work. I need selenium to wait 10 seconds. NOnot until some element is loaded or whatever, just wait 10 seconds. I know there is this
和其他事情,但它不起作用。我需要硒等待 10 秒。NO直到一些元素被加载或什么,只是等待10秒。我知道有这个
try:
element_present = EC.presence_of_element_located((By.ID, 'whatever'))
WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
print "Timed out waiting for page to load"
I do not want that.
我不想那样。
If waiting for some seconds is to much (not achievable) for selenium, what other (python) library's/programs would be capable to achieve this task? With Javas Selenium it does not seem to be a problem...
如果等待几秒钟对 selenium 来说太多(无法实现),那么其他(python)库/程序能够完成这项任务吗?使用 Javas Selenium 似乎不是问题......
回答by Gaurang Shah
All the APIs you have mentioned is basically a timeout, so it's gonna wait until either some event happens or maximum time reached.
您提到的所有 API 基本上都是超时,因此它会等到某个事件发生或达到最大时间。
set_page_load_timeout- Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
set_page_load_timeout- 设置在抛出错误之前等待页面加载完成的时间。如果超时为负,则页面加载可能是不确定的。
implicitly_wait- Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
隐式等待 - 指定驱动程序在搜索元素时应等待的时间,如果它不是立即存在的。
set_script_timeout- Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
set_script_timeout- 设置在抛出错误之前等待异步脚本完成执行的时间量。如果超时为负,则脚本将被允许无限期运行。
for more information please visit following page. (documention is for JAVA binding, but functionality should be same for all the bindings) https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#implicitlyWait-long-java.util.concurrent.TimeUnit-
欲了解更多信息,请访问以下页面。(文档适用于 JAVA 绑定,但所有绑定的功能应该相同) https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#implicitlyWait-long -java.util.concurrent.TimeUnit-
So, if you want to wait selenium (or any script) 10 seconds, or whatever time. Then the best thing is to put that thread to sleep.
因此,如果您想等待 selenium(或任何脚本)10 秒或任何时间。那么最好的办法是让该线程进入睡眠状态。
In pythonit would be
在python中它会是
import time
time.sleep(10)
In JAVA it would be
在 JAVA 中它会是
The simple way to do this is using
做到这一点的简单方法是使用
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}