user3044116
user3044116

Reputation: 11

How can I run code in selenium to execute a function at a specific hour of the day within the python script not cron

I have written a python selenium script to log me into a website. This website is where I need to book a court at precisely 7am. I cannot run the script using cron scheduler because that only runs the script and by the time selenium has logged in 7am will have passed. I've tried time() and Webdriverwait but these only allow me to delay hitting a web page button. I need to synchronise the click of a button at a precise time from within the python script.

Upvotes: 1

Views: 1218

Answers (2)

user3044116
user3044116

Reputation: 11

I've done it! The link above was of great help.

The python code of input will execute at precisely the time the new minute starts. I run the Python script using Cron at 6.59 and Python will wait until it is 7.00am and bang! the submit button is pressed by python and selenium.

My code (really the code above!):

    def WaitForNextMinute():
        secs = datetime.utcnow().second
        while secs < 57:
              sleep (57 - secs)
              secs = datetime.utcnow().second
        while datetime.utcnow().second >= 57: 
            sleep (1)


    while True:
        WaitForNextMinute()
        inputElement = driver.find_element_by_name("save_booking").click()

The thing about Python is the indent rule...annoying!!!

Upvotes: 0

the-noob
the-noob

Reputation: 1342

It has to be in combination with a cron job. You can start the cron 1-2 minutes earlier, open the login page and, in your python script, sleep until 7am then just login.

Upvotes: 1

Related Questions