ahron
ahron

Reputation: 59

is there a way to get my python selenium bot to start by itself at a certain time

im trying to get this bot to launch by itself at a certain time. i am brand new to coding so please explain your answers more thoroughly. if i can program it that on a certain date and time it would start that would be amaizing.. if you see anything else in the code that can be fixed please let me know.. thanks screenshot

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
start_time = time.time()

path = "C:\Program Files (x86)\Common Files\Chromedriver.exe"
driver = webdriver.Chrome(path)

# start action
# open page
driver.get("https://catalog.usmint.gov/andrew-jackson-presidential-silver-medal-S807.html?cgid=2020-product-schedule")
# maximize window
#driver.maximize_window()

# add to bag
while True:
    try:
        element = WebDriverWait(driver, 2).until(
        EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[3]/div[2]/div[2]/form/div/div[5]/button[1]')) #add to
    )
        break
    except:
        driver.get(driver.current_url) # refresh
    finally:
        driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/div[2]/form/div/div[5]/button[1]").click()
# checkout
while True:
    try:
        element = WebDriverWait(driver, 2).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="mini-cart"]/div[3]/div[2]/div[3]/a')) #checkout
    )
        break
    except:
        driver.get(driver.current_url) # refresh
    finally:
        driver.find_element_by_xpath('//*[@id="mini-cart"]/div[3]/div[2]/div[3]/a').click()
# login
        driver.find_element_by_xpath('//*[@id="dwfrm_login_username"]').send_keys("username")
# password
        driver.find_element_by_xpath('//*[@id="dwfrm_login_password"]').send_keys("password")
# checkout as registered user
while True:
    try:
        element = WebDriverWait(driver, 2).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="checkoutMethodLoginSubmit"]/span')) #checkout as
    )
        break
    except:
        driver.get(driver.current_url) # refresh
    finally:
        driver.find_element_by_xpath('//*[@id="checkoutMethodLoginSubmit"]/span').click()
# credit card scroll
while True:
    try:
        element = WebDriverWait(driver, 2).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCardList"]/option[2]')) #credit card scroll
    )
        break
    except:
        driver.get(driver.current_url) # refresh
    finally:
        driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCardList"]/option[2]').click()
#cvv
        time.sleep(.5)
        driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCard_cvn"]').send_keys('987')
# continue to final review
        time.sleep(.5)
while True:
    try:
        element = WebDriverWait(driver, 2).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="checkoutContinuePaymentDelegator"]')) #continue to final
    )
        break
    except:
        driver.get(driver.current_url) # refresh
    finally:
        driver.find_element_by_id("checkoutContinuePaymentDelegator").click()
# terms of use button
while True:
    try:
        element = WebDriverWait(driver, 2).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="formAgreementLabel"]/span')) #terms of use
    )
        break
    except:
        driver.get(driver.current_url) # refresh
        driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCardList"]/option[2]').click()
        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCard_cvn"]').send_keys("987")
        time.sleep(3)
        driver.find_element_by_id("checkoutContinuePaymentDelegator").click()
        time.sleep(1)
    finally:
        driver.find_element_by_xpath('//*[@id="formAgreementLabel"]/span').click()
# place order
while True:
    try:
        element = WebDriverWait(driver, 2).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="submitOrderButton"]')) #Place order
    )
        break
    except:
        driver.get(driver.current_url) # refresh
    finally:
        driver.find_element_by_xpath('//*[@id="submitOrderButton"]').click()
print("time elapsed: {:.2f}s".format(time.time() - start_time))
print("coin purchased")
driver.quit()

Upvotes: 0

Views: 694

Answers (2)

肖海飚
肖海飚

Reputation: 36

If you are tring to launch it in linux, you can use crontab. like this:

# open a cron table
$ crontab -e
# edit the table like, this means your python script while launch on the 3rd and 15th minutes of each hour
3,15 * * * * your_python_script

If you want to launch it in windows, you can use the task scheduler.

enter image description here

enter image description here

create a basic task, set the time and your script path.

enter image description here

Upvotes: 0

ErrorAssassin
ErrorAssassin

Reputation: 46

Install APSheduler API by:

pip install schedule

This is modified from their sample program:

import time

def main:
    #your program defined in a function main

schedule.every().day.at("#time").do(#yourFunction)

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

Upvotes: 1

Related Questions