简体   繁体   English

相当于 Selenium 中的 time.sleep()

[英]Equivalent of time.sleep() in Selenium

I realize this is a relatively simple question but I haven't found the answer yet.我意识到这是一个相对简单的问题,但我还没有找到答案。

I'm using driver.get() in a for loop that iterates through some urls.我在遍历某些 url 的 for 循环中使用driver.get() To help avoid my IP address getting blocked, I've implemented time.sleep(5) before the driver.get statement in the for loop.为了帮助避免我的 IP 地址被阻止,我在 for 循环中的driver.get statement之前实现了 time.sleep time.sleep(5)

Basically, I just want a wait period to make my scraping seem more natural.基本上,我只是想要等待一段时间,让我的刮擦看起来更自然。

I think time.sleep may be causing page crashes.我认为time.sleep可能导致页面崩溃。 What is the equivalent of time.sleep in selenium? time.sleep中的 time.sleep 相当于什么? From what I understand, implicitly_wait just sets the amount of time before throwing an exception, but I'm not sure that's what I want here?据我了解, implicitly_wait只是设置抛出异常之前的时间量,但我不确定这就是我想要的吗? I want a specific amount of time for the driver to wait.我想要特定的时间让司机等待。

time.sleep()时间.睡眠()

The sleep() function is from the time module which suspends execution of the current thread for a given number of seconds. sleep() function 来自时间模块,它将当前线程的执行暂停给定的秒数。

Now, WebDriver being a out-of-process library which instructs the browser what to perform and at the same time the web browser being asynchronous in nature, WebDriver can't track the active, real-time state of the HTML DOM .现在, WebDriver是一个进程外库,它指示浏览器执行什么,同时 web 浏览器本质上是异步的,WebDriver 无法跟踪HTML DOM的活动实时 state 。 This gives rise to some intermittent issues that arise from usage of Selenium and WebDriver those are subjected to race conditions that occur between the browser and the user's instructions.这会引起一些因使用SeleniumWebDriver而引起的间歇性问题,这些问题受浏览器和用户指令之间发生的竞争条件的影响。

As of now Selenium doesn't have any identical method to time.sleep() , however there are two equavalent methods at your disposal and can be used as per the prevailing condition of your automated tests.截至目前Selenium没有任何与 time.sleep time.sleep()相同的方法,但是您可以使用两种等效的方法,并且可以根据自动化测试的普遍情况使用。

  • Implicit wait : In this case, WebDriver polls the DOM for a certain duration when trying to find any element. 隐式等待:在这种情况下,WebDriver 在尝试查找任何元素时会轮询 DOM 一段时间。 This can be useful when certain elements on the webpage are not available immediately and need some time to load.当网页上的某些元素不能立即使用并且需要一些时间加载时,这会很有用。

     def implicitly_wait(self, time_to_wait) -> None: """ 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. :Args: - time_to_wait: Amount of time to wait (in seconds):Usage: :: driver.implicitly_wait(30) """ self.execute(Command.SET_TIMEOUTS, { 'implicit': int(float(time_to_wait) * 1000)})
  • Explicit wait : This type of wait allows your code to halt program execution, or freeze the thread, until the condition you pass it resolves. 显式等待:这种类型的等待允许您的代码暂停程序执行或冻结线程,直到您传递给它的条件得到解决。 As an example:举个例子:

There is no specific method in Selenium for hardcoded pauses like a time.sleep() general Python method. Selenium 中没有像 time.sleep time.sleep()一般 Python 方法那样的硬编码暂停的特定方法。
As you mentioned there is an implicitly_wait and Expected Conditions explicit WebDriverWait waits but both these are NOT a hardcoded pauses.正如您所提到的,有一个implicitly_wait和 Expected Conditions 显式WebDriverWait等待,但这两者都不是硬编码的暂停。
Both the implicitly_wait WebDriverWait are used for setting the timeout - how long time to poll for some element presence or condition, so if that condition is fulfilled or the element is presented the program flow will immediately continue to the next code line. implicitly_wait WebDriverWait都用于设置超时 - 轮询某个元素存在或条件的时间,因此如果满足该条件或元素出现,程序流将立即继续到下一个代码行。
So, if you want to put a pause you have to use some general Python method that will suspend the program / thread run like the time.sleep() .所以,如果你想暂停,你必须使用一些通用的 Python 方法来暂停程序/线程运行,如 time.sleep time.sleep()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM