1

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

  • 1
    Does this answer your question? [What is the best way to repeatedly execute a function every x seconds?](https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds) – WoAiNii May 01 '20 at 17:09
  • Have you looked into running it as a cron job? If not, this might help https://stackoverflow.com/questions/8727935/execute-python-script-via-crontab – Philippe Dixon May 01 '20 at 17:10
  • Running one-off jobs like this repeatedly or at certain times is generally the job of things like cron or systemd, but they might be troublesome to use here due to the need for a graphical context. – FallenWarrior May 01 '20 at 17:10
  • True @FallenWarrior, perhaps Selenium could be run headless if it's a problem. – Philippe Dixon May 01 '20 at 17:12
  • Try also the [timeloop library](https://pypi.org/project/timeloop/) – Wippo May 01 '20 at 17:14
  • Sleep() method is so inefficient... I was hoping no one would mention it – Wippo May 01 '20 at 17:18

3 Answers3

0

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.

match
  • 10,388
  • 3
  • 23
  • 41
  • what code goes in here? from PATh to driver quit or do I also need to put the import stuff inside this loop? – charlie brown May 01 '20 at 17:16
  • 1
    and instead of writing 60 *60 can i just use the seconds like 3600? – charlie brown May 01 '20 at 17:18
  • You only need the `driver.get()` and later code - the 'setup' code at the start (`import` etc) only needs to happen once. You can use 3600 - I just illustrated it that way in case you want to make a 'minutes' variable later. – match May 02 '20 at 14:29
0

You can use thread sleep like this in Python.

import time
while True:
   #Your Code
   time.sleep(60)
Sanjuwa
  • 43
  • 1
  • 6
0

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.