python - python自动化:时间不增加
问题描述
我正在使用 selenium 和 pyautogui 以及文本文件构建一个自动缩放启动器项目来保存数据,并且我使用循环来检查它是真还是假。文本文件如下所示。
Thursday,Test,12:24,Yes,Link,zoom_link Thursday,Test2,8:30,Yes,Link,zoom_link 我将文本文件的内容转换为列表并将其放入数据列表中,因此看起来像这样
data = [['Thursday','Test','12:24','Yes','Link','zoom_link'], ['Thursday','Test2','8:30','Yes','Link','zoom_link']]
这是与问题相关的代码
for record in data:
convert_time_record = datetime.datetime.strptime(record[2], '%H:%M').time()
date_now = datetime.datetime.now()
# Datetime and auto validation for web automation
while True:
if record[3] == "Yes":
if record[4] == "Link":
if record[0] == date_now.strftime('%A') and convert_time_record == date_now.strftime('%H:%M:%S'):
driver = webdriver.Chrome()
driver.get(record[5])
try:
element = WebDriverWait(driver, 15).until(
ec.presence_of_element_located((By.CLASS_NAME, "_3Gj8x8oc"))
)
element.click()
time.sleep(2)
open_meeting_btn = pyautogui.locateCenterOnScreen('open_zoom_web.png')
pyautogui.moveTo(open_meeting_btn)
pyautogui.click()
finally:
driver.close()
print('link action')
break
# Check if the method was by meeting ID
elif record[4] == "Meeting ID":
if record[0] == date_now.strftime('%A') and convert_time_record == date_now.strftime('%H:%M:%S'):
# Open Zoom
subprocess.call(zoom_path)
time.sleep(3)
# Locate the center of the join button then move the cursor
join_button = pyautogui.locateCenterOnScreen('join_button.png')
# Move the cursor to the location
pyautogui.moveTo(join_button)
# Click the button
pyautogui.click()
time.sleep(3)
# Write the meeting id to the text field
pyautogui.write(record[5])
# Press the enter key
pyautogui.press('enter')
time.sleep(3)
# Write the passcode to the text field
pyautogui.write(record[6])
# Press the enter key
pyautogui.press('enter')
print('id action')
break
用 Python IDLE 测试后,我发现为什么条件从未满足
import datetime
>>> x=datetime.datetime.now()
>>> data = [['Thursday', 'Test', '14:34', 'Yes', 'Link', link]]
>>> for record in data:
convert = datetime.datetime.strptime(record[2],'%H:%M').time()
while True:
if record[0]==x.strftime('%A') and convert==x.strftime('%H:%M:%S'):
print('true')
break
else:
print('false', convert, x.strftime('%H:%M:%S'))
这是打印结果
false 14:34:00 14:32:00
false 14:34:00 14:32:00
false 14:34:00 14:32:00
false 14:34:00 14:32:00
false 14:34:00 14:32:00
false 14:34:00 14:32:00
false 14:34:00 14:32:00
false 14:34:00 14:32:00
# ...
时间没有增加,我该x=datetime.datetime.now()
如何解决?
解决方案
x
x=datetime.datetime.now()
每次调用此函数时都会设置 in 。在您的示例中,您似乎只将 x 设置为当前时间一次。如果您不再调用该函数datetime.datetime.now()
,并设置x
为当前时间,x 将保持不变。
x=datetime.datetime.now()
每次需要当前时间时都需要运行此行。在这个例子中,我们在 for 循环中调用datetime.datetime.now()
了 5 次。
import datetime
import time
for i in range(5):
x=datetime.datetime.now()
print("x = ", x)
time.sleep(1)
Out [2]:
x = 2020-10-15 10:27:26.001649
x = 2020-10-15 10:27:27.009904
x = 2020-10-15 10:27:28.015560
x = 2020-10-15 10:27:29.024214
x = 2020-10-15 10:27:30.031730
推荐阅读
- assembly - 什么是 gdb 中的“堆栈”部分
- javascript - 出现错误:verifyIdToken() 的第一个参数必须是 Firebase ID 令牌字符串
- f# - F# Idiomatic Index 打印索引时加一
- ios - 在 Apple 的 Foundation/Swift/Objective-C 中,runLoop.run 如何阻塞,但仍允许 DispatchWorkItems 处理?
- python - 启动 Jupyter Notebook 时 Python 3 失败错误
- css - 如何在 React scss 模块中使用 scss @use
- typescript - 如何在 Typescript 中将空接口指定为通用参数?
- parsing - LR 解析如何选择合格的语法产生式(从叶子构造解析树)?
- razor - 是否可以通过 Razor 页面中的 PageRoute 或约定匹配所有子目录?
- javascript - JavaScript 控制台游戏积分系统