本文介绍了python和selenium webdriver等到时间等于特定时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的python 3用户.

我有一个通过python 3进行的项目网络抓取,我必须等到登录到网络目标后的时间是晚上08:22:00.

I have a project web scraping via python 3 which I have to waiting until time will be 08:22:00 PM after login to web target.

我有项目,但没有任何问题,但我只想导入直到特定时间再继续.

I have project and not any problem but I want import only waiting until specific time and continue again.

您有任何想法吗,或者您可以告诉我有关该代码的任何代码,例如:

Do you have any idea or can you show me any code about that for example :

WebDriverWait(driver, 10).until() # for example time=08:22:00 pm continue

谢谢

推荐答案

如果您查看 WebDriverWait 定义为:

class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
Constructor, takes a WebDriver instance and timeout in seconds.

进一步,直到直到方法被定义为:

Further the untill method is defined as :

until(method, message='')
Calls the method provided with the driver as an argument until the return value is not False.

因此 WebDriverWait 构造函数和 until 方法都与 WebDriver 实例相关联,该实例广泛用于与之通信浏览器客户端.因此, WebDriver 可能对您没有帮助.

So both WebDriverWait constructor and until method is associated with the WebDriver instance which is extensively used to communicate with Browser Clients. Hence WebDriver may not help you.

说过,Python提供了不同的解决方案.

Having said that different solutions are available through Python.

您可以导入 时间 ,然后模块的 datetime 模块> Python sleep(),如下所示:

You can import the time and datetime module of Python to sleep() in a simple loop as follows :

import datetime
import time

# datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
# The year, month and day arguments are required. tzinfo may be None.
target_time = datetime.datetime(2018, 1, 23, 13, 2, 00)  # 13:02 pm on 23 January 2018
while datetime.datetime.now() < target_time:
    time.sleep(10)
print("It is 13:02 pm, 2018. Resuming Test Execution")


解决方案2

您可以导入 计时器 > 线程 中的对象 Python 的>模块,以引起timer如下:


Solution # 2

You can import the Timer object from threading module of Python to induce a timer as follows :

from threading import Timer

def hello():
    print("Hello World")

t = Timer(10.0, hello)
t.start()  # after 30 seconds, "Hello World" will be printed


琐事

您还使用事件计划程序 sched 实现了通用事件调度程序:


Trivia

You also use the Event scheduler Class sched which implements a general purpose event scheduler:

class sched.scheduler(timefunc=time.monotonic, delayfunc=time.sleep)

这篇关于python和selenium webdriver等到时间等于特定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:22
查看更多