python自动化:时间不增加
python automation: time not increasing
我正在用 selenium 和 pyautogui 以及文本文件构建一个自动缩放启动器项目来保存数据,我使用循环来检查它是对还是错。文本文件如下所示。
星期四,测试,12:24,是,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=datetime.datetime.now()
时,您正在调用函数并且 x 是对结果的引用。所以当您以后使用它时,x 将始终是相同的日期。因此 x.strftime('%A')
总是会给你同样的结果。
您想要的是引用该函数,以便在每次需要时调用它。在 python 中,您可以像使用值一样对变量使用赋值函数。
所以你可以做 x=datetime.datetime.now
(这里没有括号)然后用 current_time=x()
或 current_time_string= x().strftime('%H:%M:%S')
.
调用 x
每次调用此函数时,x
in x=datetime.datetime.now()
都会设置。在您的示例中,您似乎只将 x 设置为当前时间一次。如果你不再调用函数 datetime.datetime.now()
,并将 x
设置为当前时间,x 将保持不变。
每次需要当前时间时,都需要运行这一行x=datetime.datetime.now()
。在此示例中,我们在 for 循环中调用了 5 次 datetime.datetime.now()
。
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
我正在用 selenium 和 pyautogui 以及文本文件构建一个自动缩放启动器项目来保存数据,我使用循环来检查它是对还是错。文本文件如下所示。
星期四,测试,12:24,是,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=datetime.datetime.now()
时,您正在调用函数并且 x 是对结果的引用。所以当您以后使用它时,x 将始终是相同的日期。因此 x.strftime('%A')
总是会给你同样的结果。
您想要的是引用该函数,以便在每次需要时调用它。在 python 中,您可以像使用值一样对变量使用赋值函数。
所以你可以做 x=datetime.datetime.now
(这里没有括号)然后用 current_time=x()
或 current_time_string= x().strftime('%H:%M:%S')
.
x
in x=datetime.datetime.now()
都会设置。在您的示例中,您似乎只将 x 设置为当前时间一次。如果你不再调用函数 datetime.datetime.now()
,并将 x
设置为当前时间,x 将保持不变。
每次需要当前时间时,都需要运行这一行x=datetime.datetime.now()
。在此示例中,我们在 for 循环中调用了 5 次 datetime.datetime.now()
。
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