简体   繁体   中英

How can I repat a script every 60 minutes with python

Im using following script to automate a task on a website

How can i make it so that this script runs every 60 minutes?

Im using this code for my task which works when i manually run this but i want to run this script once and than repeat it every 60 minutes automatically

Here is the code I use

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

PATH = "/usr/bin/chromedriver"

driver = webdriver.Chrome(PATH)

driver.get("https://freebitco.in")
driver.maximize_window()
time.sleep(2)
driver.find_element_by_link_text('LOGIN').click()
time.sleep(3)
driver.find_element_by_id("login_form_btc_address").send_keys("EMAILADDRESS")
driver.find_element_by_id("login_form_password").send_keys("PASSWORD")
driver.find_element_by_id("login_button").click()
time.sleep(4)
driver.find_element_by_class_name("pushpad_deny_button").click()
time.sleep(3)
driver.find_element_by_id("free_play_form_button").click()
time.sleep(5)
driver.find_element_by_class_name("close-reveal-modal").click()
driver.quit()

I want to repeat this script every 60 minutes

Inside the code, you can achieve this with a long sleep:

while True:
  # existing code goes here...
  time.sleep(60 * 60) # secs x mins

You might want to change the while condition to something else if you need to test for and stop in certain circumstances.

You can use thread sleep like this in Python.

import time
while True:
   #Your Code
   time.sleep(60)

First put all the code inside a function. Then use timer function of threading module as below to repeat function after certin time interval.

import threading

def your_function():
  #your code inside function goes here. Make sure the below line is at last.
  threading.Timer(5.0, your_function).start()


your_function

# continue with the rest of your code

Replace 5.0 with your desired time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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