如果您查看WebDriverWait 的 API Docs,它的定义是:
class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
Constructor, takes a WebDriver instance and timeout in seconds.
进一步将直到方法定义为:
until(method, message='')
Calls the method provided with the driver as an argument until the return value is not False.
所以 WebDriverWait 构造函数和 until 方法都与 WebDriver 实例相关联,该实例广泛用于与浏览器客户端。因此 WebDriver 可能对您没有帮助。
话虽如此,通过Python 可以获得不同的解决方案。
解决方案#1
您可以通过简单的循环将Python的time和datetime模块导入sleep():
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的threading模块中导入Timer对象来诱导timer如下:
from threading import Timer
def hello():
print("Hello World")
t = Timer(10.0, hello)
t.start() # after 30 seconds, "Hello World" will be printed
琐事
您还可以使用 Event scheduler 类 sched,它实现了一个通用的事件调度程序:
class sched.scheduler(timefunc=time.monotonic, delayfunc=time.sleep)