【问题标题】:python and selenium webdriver wait until time was equal to specificpython和selenium webdriver等到时间等于特定
【发布时间】:2018-01-22 20:44:16
【问题描述】:

我是python 3 新用户。

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

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

你有什么想法吗,或者你能告诉我任何代码吗?例如:

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

谢谢

【问题讨论】:

  • Selenium 条件等待并返回一个元素,那么这种情况下的元素是什么?似乎您只是在等待特定的时间,所以为什么不直接等待stackoverflow.com/questions/6579127/… 的讨论?
  • 如果你只是在抓取,为什么不在晚上 8:22 开始脚本呢?
  • @MivaScott,因为如果您尝试登录 8:22 站点,如果您没有登录,则不会加载页面

标签: python python-3.x selenium selenium-webdriver


【解决方案1】:

如果您查看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

您可以通过简单的循环将Pythontimedatetime模块导入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

您可以从Pythonthreading模块中导入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 schedulersched,它实现了一个通用的事件调度程序:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 2017-02-21
    • 2014-12-21
    • 2014-04-04
    相关资源
    最近更新 更多