Skip to main content

How to Buy a Playstation 5 in Your Sleep

·5 mins

A Primer on Creating Web Bots with Selenium in Python #

Do you want a Playstation 5 but

  • you are tired of constantly hitting the “Refresh”-button of your internet browser to check if the shop has it in stock,
  • it is always sold out one minute after some online shop offered it,
  • you are totally annoyed by software developers who created those bots that buy all the Playstations in no time?

Well, why not fight fire with fire and write your own bot?

This post is a tutorial on how to create a web bot with Selenium in Python. This bot will constantly check a product page for the Playstation 5 of a popular web shop in Germany.

I have made a video showing two scenarios. In the first, the browser periodically refreshes and checks the product page of the Playstation 5 if it is available. Of course, at this time it is not available. Therefore, to demonstrate the rest of the purchasing process, I have chosen a product that is available.

https://youtu.be/LSBEMbfQMww

Don’t worry, if you need a bot for another shop. The code is so simple and can easily be adapted. There are some links in the “Resources”-section at the end of the document which might help you getting deeper into the matter.

The code can be downloaded from here: https://github.com/twissmueller/playstation-bot/tree/master

Disclaimer: Please check the license inside the code repository. In short: The code is provided as is. You can do whatever you want with the code but don’t blame me if you accidentally cleared your bank account.

What you need is a computer with

  • Python 3 and PIP
  • Chrome and ChromeDriver

Please make sure, that your Chrome version fits the one of ChromeDriver.

$ chromedriver --version
ChromeDriver 94.0.4606.41 (333e85df3c9b656b518b5f1add5ff246365b6c24-refs/branch-heads/4606@{#845})

You can get the code from this link: TODO GITHUB LINK

When everything is in its place change into the directory and activate the Python environment.

cd playstation-bot
source bin/activate

Then the requirements can be installed with:

python3 -m pip install -r requirements.txt

Now, edit the script and provide the username and password for the shop and or course the right product that you want to purchase. Please see lines 6 to 9.

Finally, the script can be run:

python3 app.py

Now, depending on which product is being addressed, the script either refreshes the page periodically or puts the product into the basket and continues with the checkout process.

There is one caveat though, the script does not run through the payment process because this depends on the preferred payment option. Additionally, some payment providers like Paypal might use 2-factor authentication which makes it even more complicated.

Therefore, to implement the rest some more work is required, but at least the product is already in the basket. Feel free to fork the repo and do what has to be done.

How Does It Work? #

Let’s get a little bit into the technical details.

What is Selenium and how can it be used to buy a Playstation?

From the Selenium website:

Selenium is a suite of tools for automating web browsers.

Is is a simple as that.

It mimics the user so we can automate certain tasks, e.g. testing of complete websites.

Breaking it down, in this tutorial, we emulate three simple tasks that a user normally does when visiting a website:

  • open the browser and navigate to the product page
  • click a few buttons
  • fill in a form

Easy, there is not much more behind it. Really.

The code snippet to open the product page is

PRODUCT = "<product_link"
driver = webdriver.Chrome()
driver.get(PRODUCT)

Just the PRODUCT-variable needs to be changed in order to adapt the script to another product. Xbox anyone?

Now, the browser with the product page is open and the next steps, e.g. checking if the “Add to Cart”-button can be pressed, can be executed.

An element on a page can easily be addressed via its ID, e.g.

element = driver.find_element_by_id("mms-login-form__email")

What if an element has no ID? Then we can select the element via its XPATH. In the code this is being done in the function for clicking a button:

element = driver.find_element(By.XPATH, f'//button[text()="{button_text}"]')

With the code element.click() the button gets “clicked”, but sometimes this would not work on the page which had to do with how the developers used JavaScript for their buttons.

Therefore I had to add a different code for clicking a button:

driver.execute_script("arguments[0].click();", element)

Finally, the last action that I want to address is how to fill-in a form-element, e.g. the one for the email-address.

driver.find_element_by_id("mms-login-form__email").send_keys(username)

There might be one last question, though. How do we know the IDs of those elements?

Open Chrome on the page with the element you try to identify, right click and click “Select”. Than check for the “id”-field of this element.

Then, a side-bar opens showing the source with the selected element.

What if there is no id-tag? Do another right-click and select “Copy -> XPath”.

Conclusion #

I hope this tutorial has given you some insights on how to automate browser tasks for various purposes. Sorry, for luring you into the field of web-frontend testing by using a “clickbaity” title. There is really not much to the code and it is easy to understand, right?

Anyways, hope you have enjoyed this tutorial!

Feel free to buy me a coffee if you liked this post.

Done for today!

Resources #