Schedule library starts 20-46 Seconds after the time I set, Not sure why

Viewed 25

Trying to run a program every day at exactly 11:59:40 AM when I use the schedule library it will start 20+ Seconds after the time I set, Not sure why. Is there another library I can use? Also tried using Task Scheduler and it says it's running but doesn't perform the actions like it does when running it in Pycharm.

import schedule
import time
import webbrowser
from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController

url = "https://walmart.com"

def open_site():
    webbrowser.open(url, new=1)
    time.sleep(5)
    keyboard = KeyboardController()
    mouse = MouseController()

    #### Logs in to Site ###
    mouse.position = (800, 530)
    keyboard.press(Key.ctrl_r)
    keyboard.release(Key.ctrl_r)
    mouse.press(Button.left)
    mouse.release(Button.left)
    time.sleep(2)
    keyboard.press(Key.down)
    keyboard.release(Key.down)
    time.sleep(1)
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    time.sleep(1)
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)

schedule.every().day.at("11:59:40").do(open_site)

while True:
    schedule.run_pending()
    time.sleep(60)  # wait one minute

1 Answers

I didn't realize that the script was starting at the bottom and then going to the top of def open_site. I placed:

print("Program Started on " + strftime("%a, " + "at " + "%d %b %Y %H:%M:%S +0000", gmtime())) Above webbroswer.open(url, new=1)

and then:

print("Program Ended on " + strftime("%a, " + "at " + "%d %b %Y %H:%M:%S +1000", gmtime()))

below the last keyboard.release(Key.enter) then I moved the: time.sleep(60) below that. When that first print statement was Program Ended on I found the problem. Basically, I used from time import gmtime, strftime to see when the script was starting and ending. I love the EUREKA!!!! moments I get with learning this language. Here is the updated code.


import schedule
import time
import webbrowser
from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController
from time import gmtime, strftime

url = "https://walmart.com"

def open_site():
    webbrowser.open(url, new=1)
    time.sleep(5)
    keyboard = KeyboardController()
    mouse = MouseController()

    #### Logs in to Site ###
    mouse.position = (800, 530)
    keyboard.press(Key.ctrl_r)
    keyboard.release(Key.ctrl_r)
    mouse.press(Button.left)
    mouse.release(Button.left)
    time.sleep(2)
    keyboard.press(Key.down)
    keyboard.release(Key.down)
    time.sleep(1)
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    time.sleep(1)
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    time.sleep(60)
    return

schedule.every().day.at("23:36:00").do(open_site)

while True:
    schedule.run_pending()
    time.sleep(.01)
Related