How to Create a Python Bot to Auto-Buy a GPU Using Selenium Easily!


    With the high demand for graphics cards (GPUs) and limited supply, purchasing one before it sells out can be a challenge. Using Python and Selenium, you can automate the buying process to increase your chances of securing a GPU. In this guide, we will walk through the steps to create a Python bot that will automatically check for stock, add a GPU to the cart, and proceed with checkout.


1. Prerequisites

Before starting, ensure you have the following installed:

  • Python 3.x (Download from Python.org)
  • Google Chrome and ChromeDriver (Ensure the ChromeDriver version matches your Chrome version: Download here)
  • Selenium library (Install via pip install selenium)

2. Setting Up the Environment

First, install the required Python libraries:

pip install selenium

Then, download and place the ChromeDriver in a known directory.


3. Writing the Python Bot

Below is a simple Python script that automates adding a GPU to the cart from an online store like Best Buy or Amazon.

Step 1: Import Dependencies

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

Step 2: Configure the Web Driver

driver_path = "path/to/chromedriver"  # Update this with your actual path
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--start-maximized")

driver = webdriver.Chrome(executable_path=driver_path, options=options)

Step 3: Navigate to the Product Page

gpu_url = "https://www.bestbuy.com/site/some-gpu-url"
driver.get(gpu_url)
time.sleep(2)

Step 4: Check Availability and Add to Cart

while True:
    try:
        add_to_cart_button = driver.find_element(By.CSS_SELECTOR, ".add-to-cart-button")
        if add_to_cart_button.is_enabled():
            add_to_cart_button.click()
            print("GPU added to cart!")
            break
        else:
            print("GPU out of stock. Refreshing...")
            driver.refresh()
            time.sleep(3)
    except:
        print("Error finding the button. Retrying...")
        driver.refresh()
        time.sleep(3)

Step 5: Proceed to Checkout

checkout_url = "https://www.bestbuy.com/cart"
driver.get(checkout_url)
time.sleep(2)

checkout_button = driver.find_element(By.CSS_SELECTOR, ".checkout-buttons__checkout")
checkout_button.click()
print("Proceeding to checkout...")

Step 6: Auto-Fill Login and Payment Details (Optional) 

To automate login:
email_input = driver.find_element(By.ID, "fld-e")
password_input = driver.find_element(By.ID, "fld-p1")

email_input.send_keys("your-email@example.com")
password_input.send_keys("your-password")
password_input.send_keys(Keys.RETURN)

print("Logged in successfully.")

For credit card details, it is not recommended to store sensitive information in a script. Instead, use a password manager or manually complete checkout. 

4. Running the Bot 

Save the script as gpu_bot.py and run:
python gpu_bot.py
The bot will continuously refresh the page and attempt to buy the GPU once available. 

5. Tips for Success 

Use Headless Mode: Add options.add_argument("--headless") to run the bot without opening a browser. Set Custom Wait Times: Adjust time.sleep(x) to avoid bot detection. Use Proxies or VPN: Prevent getting blocked for frequent requests. Check Site HTML: Some websites use dynamic elements; inspect them using Chrome Developer Tools (F12).

Post a Comment

Previous Post Next Post