LordStephan
LordStephan

Reputation: 11

Is there a way to execute a part of a PYTHON script only at a given time

I write a bot that should automatically register me for an exam. The bot should be faster than a human especially the to last lines(will add them below)

I will open the python script a few minutes before the time it need to be executed.

I am looking for a solution to the following problem:

My code (its in production I will change the username and password variable to not be plaintext).

I will also wrap it in functions.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from datetime import datetime
import time 

path = "my_path"
options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=my_user-data-dir")

driver=webdriver.Chrome(path, chrome_options=options)

time_now = datetime.now()

driver.get("linkt_to_website")


login = driver.find_element_by_class_name("Login")
login.click()

user = driver.find_element_by_id("username")
user.send_keys("my_username")


pwd = driver.find_element_by_id("password")
pwd.send_keys("my_password")


lgnbtn = driver.find_element_by_id("loginbutton")
lgnbtn.click()


driver.get("website linkt to the page with the button to register for my exam")

time.sleep(1)

toggle = driver.execute_script("js function to trigger dropdown menue")

# until here it does not make a difference how much time the bot need 

until here everything works fine the goal is that the bot logs in and wait until a predefined time(hour, minutes, seconds, milliseconds)

but now I have a question:

# the two lines below are the most important

anmed= driver.find_element_by_xpath("register button xpath ").click()

last_btn= driver.find_element_by_xpath("are you sure register button ").click()

The two button I want to trigger in the two lines above will appear at a given time so I need to make a function to trigger the two lines when its for example exactly 10:00:00 in the morning.

I hope this question makes sense.

The main goal is to start the script a few minutes before "10:00:00"AM it logs in and wait until 10AM and as accurately as possible execute the two last lines to actually register for my exam.

Thank you in advance:)

Upvotes: 1

Views: 177

Answers (2)

Correy Koshnick
Correy Koshnick

Reputation: 211

Are you sure you don't need to reload the page when for the button to become active? If not, this is a quick and dirty way to use time and datetime to check if the moment is right, and if not, wait (1 second) and try again later. You could check every 0.1 seconds if you think that would be more successful.

You might also want to use a "try" block incase the sync between your computer time and server time don't agree and so you look for a button that does not exist yet and trigger an error thus disrupting your code.

import datetime
import time

trigger_time = datetime.datetime(2021, 2, 9, 22, 0, 0) # Tonight at 10PM

while True:
    now = datetime.datetime.now()

    if now >= trigger_time:
        try:
            # RUN CODE
        except WHATEVEREXCEPTION_SELENEIUM_THROWS_FOR_MISSING_ELEMENTS:
             pass
        if code_success:
            break
    else:
       time.sleep(1)  # Wait for 1 second

Upvotes: 0

Bobby Ocean
Bobby Ocean

Reputation: 3328

If you want to pause your code until a particular time, then you essentially want your code to sleep for N seconds where N has been calculated to land at the time you want.

import time

#Time right now in seconds from epoch. 
c = time.time() 
#Time at 6:10 PM today from epoch. 
n = time.mktime(time.strptime('2021:02:09-18:10:00','%Y:%m:%d-%H:%M:%S'))

#Pause code for seconds until it reaches 6:10 PM today.
sleep(n-c) 

#Run code after pausing. 

I am EST time, so things might be different for you.

Upvotes: 1

Related Questions