TheNoobProgrammer
TheNoobProgrammer

Reputation: 1121

python automation: time not increasing

I'm building an automatic zoom launcher project with selenium and pyautogui and text file to save the data and i use loops to check if it's true or false. The text file looks like this.

Thursday,Test,12:24,Yes,Link,zoom_link Thursday,Test2,8:30,Yes,Link,zoom_link I convert the content of the text file into a list and put it in the data list so it looks something like this

data = [['Thursday','Test','12:24','Yes','Link','zoom_link'], ['Thursday','Test2','8:30','Yes','Link','zoom_link']]

This is the code related to the problem

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

After testing with the Python IDLE, i found why the conditions were never met

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'))

and this is the print result

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
# ...

the x=datetime.datetime.now() time is not increasing, how can i fix this?

Upvotes: 1

Views: 81

Answers (2)

Andre
Andre

Reputation: 788

x in x=datetime.datetime.now() gets set every time you call this function. In your example you seem to only set x to be the currtent time once. If you do not call the function datetime.datetime.now() again, and set x to be the current time, x will stay the same.

You need to run this line x=datetime.datetime.now() every time you want the current time. In this example we call datetime.datetime.now() 5 times in the for loop.

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

Upvotes: 2

HrkBrkkl
HrkBrkkl

Reputation: 673

When you do x=datetime.datetime.now() you are calling the function and x is a reference to the result. So x will always be the same date when you later use it. As a consequence x.strftime('%A') will always give you the same result. What you want is to have a reference to the funciton to call it everytime you need it. In python you can use assign funcitons to variables just like values. So you can do x=datetime.datetime.now (No brackets at the end here) and then call x with current_time=x() or current_time_string= x().strftime('%H:%M:%S').

Upvotes: 1

Related Questions