简体   繁体   中英

How to run a function using schedule in python

I want to run the following at a specific time. I looked around how to use this package, but somehow it does not work for me. Everythin in the function works fine, but when I want to combine it with schedule it does not work.

   from selenium import webdriver
   from selenium.webdriver.support.ui import Select
   import schedule
   import time
   from selenium.webdriver.support.ui import WebDriverWait
   from selenium.webdriver.common.by import By
   from selenium.webdriver.support import expected_conditions as EC
   from datetime import date




   def job():
   path = "C:\Program Files\chromedriver.exe"
   driver = webdriver.Chrome(path)
   driver.get("https://www.zssw.unibe.ch/usp/zms/angebot/5849/index_ger.html")  # link for 
   desired sport
   driver.maximize_window()
   wait = WebDriverWait(driver, 30)
   pathanmelden = driver.find_element(by=By.XPATH,
                               value="//* 
   [@id='content']/section/div/div/div/div/div/table/tbody/tr[5]/td[2]/a")
   pathanmelden.click()
   time.sleep(1)
   pathforstudents = driver.find_element(by=By.XPATH, 
   value="/html/body/div/div[2]/div[2]/form/input")
   pathforstudents.click()
   wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img#user_idp_iddicon"))).click()
   time.sleep(2)
   wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@title='Universitäten: Universität 
Bern']"))).click()
time.sleep(5)
clickanmelden = driver.find_element(by=By.XPATH, value="//*[@id='wayf_submit_button']")
clickanmelden.click()
time.sleep(2)
emailadress = ""
password = ""
personaldataemail = driver.find_element(by=By.XPATH, value="//*[@id='username']")
personaldataemail.send_keys(emailadress)
personaldatapass = driver.find_element(by=By.XPATH, value="//*[@id='password']")
personaldatapass.send_keys(password)
clickanmelden2 = driver.find_element(by=By.XPATH, value="//*[@id='login-button']")
clickanmelden2.click()
clickanmelden3 = driver.find_element(by=By.XPATH, value = "//* 
[@id='form']/div[7]/div/div/button")
clickanmelden3.click()

schedule.every().seconds(10).do(job)

while True:
schedule.run_pending()
time.sleep(1)

My personal experience with such things has led me to always schedule such activities with a 3rd party app, for a very long time now: Quartz.

It really does take the guesswork out of a lot of things as it is a separate process from your intake and shaping of the data. Plus, as they have a Jython entrypoint, there aren't too many reasons to not at least try it....

If nothing else, it also insulates your scheduling activities from everything else about your process so that will make debugging easier.

How do I use the quartz scheduler with Python?

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