-
-
Save D3ISM3/0de7f6892bf51e11fb14c6c30ae9ef17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import sys | |
from datetime import datetime | |
from selenium import webdriver | |
from selenium.common.exceptions import NoSuchElementException | |
''' | |
This bot makes a few assumptions: | |
- You have a payment method already setup | |
- You have a valid shipping address already setup | |
- You won't encounter any popups/additiona login checks | |
- If this happens, you will need to modify the code to handle this | |
''' | |
def main(wait_time, product, cvv): | |
# Initialize Browser | |
profile = webdriver.FirefoxProfile() | |
profile.set_preference('general.useragent.override', | |
'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0') | |
browser = webdriver.Firefox(profile) | |
# Navigate to target website and wait for user to login | |
browser.get("https://www.bestbuy.com/") | |
input('When you are logged into BestBuy press ENTER to continue.') | |
# After user has logged in, navigate to the product that we | |
# want to try and purchase | |
browser.get(product) | |
# Loop until we're able to purchase the product | |
complete = False | |
while (not complete): | |
# Use a try/catch to help determine if purchase was successful | |
# We expect that Selenium will throw exceptions if | |
# any element does not exist. | |
try: | |
addToCartElement = browser.find_element_by_xpath( | |
"//button[@class='c-button c-button-primary c-button-lg c-button-block c-button-icon c-button-icon-leading add-to-cart-button']") | |
addToCartElement.click() | |
browser.get('https://www.bestbuy.com/cart') | |
cartCheckoutElement = browser.find_element_by_xpath( | |
"//button[@class='btn btn-lg btn-block btn-primary']") | |
cartCheckoutElement.click() | |
freeShippingRadioElement = browser.find_element_by_xpath( | |
"//input[@id='fulfillment-shipping-4tqhbezgfdmgc-4sm0ivsy5jjyi']") | |
freeShippingRadioElement.click() | |
cvvElement = browser.find_element_by_xpath("//input[@id='cvv']") | |
cvvElement.sendKeys(cvv) | |
placeOrderElement = browser.find_element_by_xpath( | |
"//button[@class='btn btn-lg btn-block btn-primary button__fast-track']") | |
placeOrderElement.click() | |
complete = True | |
# Handle the expected exception by printing useful information and waiting | |
# before trying again | |
except NoSuchElementException: | |
print('[' + str(datetime.now()) + '] Not in stock... waiting ' + | |
str(wait_time) + ' seconds...') | |
time.sleep(wait_time) | |
browser.get(product) | |
# Gracefully end if the user ends the bot with CTRL+C | |
except KeyboardInterrupt: | |
print("Shutting down bot...") | |
break | |
# Gracefully end if any unexpected exception occurs | |
# Output exception to screen. | |
except Exception as ex: | |
print(str(ex)) | |
break | |
if __name__ == '__main__': | |
if len(sys.argv) < 4: | |
sys.exit( | |
f'Usage: python3 {sys.argv[0]} <WAIT_TIME_IN_SECONDS> <BESTBUY_PRODUCT_WEB_URL> <CVV>') | |
wait_time = int(sys.argv[1]) | |
product = str(sys.argv[2]) | |
cvv = str(sys.argv[3]) | |
main(wait_time, product, cvv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment