繁体   English   中英

python自动化:时间不增加

[英]python automation: time not increasing

我正在使用 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=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中的x=datetime.datetime.now()都会被设置。 在您的示例中,您似乎只将 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM