Creating a Python Bot with Selenium

David Mentgen
TestingOnProd
Published in
3 min readNov 14, 2021

In this post, I’m going to show you how to create a basic bot that’s capable of automating just about any mundane task you need done!

What can this bot do?

This kind of bot will have such a wide range of possibilities because it is going to be able to do most anything that YOU can do inside of a web browser. To give you some ideas of what you could use this bot for, I’ve put together a list of popular potential use cases:

  • Scraping information from the web
  • Monitoring a store page and purchasing an item when it comes in stock
  • Paying your bills
  • Sending emails
  • Playing chess
  • Most anything you can do in a browser!

We need a specific case to learn with as an example though, so I’ll show you how to code a bot to monitor an out-of-stock product on BestBuy.com, and then purchase that item when it becomes available.

Planning

Before we get into the weeds with the code, let’s discuss the planning phase. This is a fairly complex job; our bot needs to be able to walk through all of the same steps that a human would perform in a web browser to complete the same task… a nd we go through a lot of these steps subconsciously! We need to understand what’s necessary to accomplish our end goal on a more granular level so that we know what to tell the bot to do, and when to do it.

So, let’s map out the series of individual steps that need to happen in order for this to work:

  1. Login to BestBuy.com
  2. Visit product page
  3. Check if product is available
  4. If product is not available, wait X amount of time until refresh
  5. If product is available, purchase item
  6. Walk through steps of purchasing item with card that is already saved to BestBuy account.
  7. If purchase was not successful, loop back to step 3.
  8. If purchase was successful, end

Code

Running the Code

You can run the code using the following:
python3 Bot.py <TIME_IN_SECONDS> <BESTBUY_PRODUCT_WEB_URL> <CVV>

Example:
python3 Bot.py 10 https://www.bestbuy.com/site/evga-rtx-3080-xc3-ultra-gaming-10g-p5-3885-kh-pci-express-4-0-lhr/6471615.p?skuId=6471615 1234

The previous command will point our bot to the provided product page. If the product is out of stock, the bot will wait 10 seconds before checking again. If the product is in stock, then the bot will proceed to purchase the item. To do this, it will have to enter 1234 into the expected CVV field for the card that you have already setup with your BestBuy account.

Troubleshooting

This code relies on several assumptions:

  • The customer account is setup with a valid payment method
  • The account is setup with a valid shipping address
  • The browser will not encounter any additional login checks
  • The browser will not encounter any popups
  • BestBuy has not updated their website since this code was made

If popups or additional checks do occur, you should be able modify the code to take these into account. For example, BestBuy likes to throw survey popups for users. If this happens, you can add to the code to have it check for the presence of a popup, and then have it click the “X” in the top right based on its path if there is one.

Learn More

If you enjoyed this, consider following me over on WordPress or here on Medium! I write a new post every week and I would really appreciate your support. Here are a few of my previous posts that you may also be interested in:

Originally published at http://testingonprod.com on November 14, 2021.

--

--