DEV Community

Cover image for Flash Sale magic

Posted on • Updated on

Flash Sale magic

Its a python script that automates to buy the product from amazon(can be anything with little changes).
This bot is created to explore selenium in python

what is a flash sale? Its a kind of sale where stocks will be limited and get cleared in minutes

All I wanted to buy a product from amazon which was in flash sales. So thought of creating a python script for automating.

Alt text of image


Selenium is my go-to tool for automating with python, with the help of selenium we first open the amazon login link. Once after logging in, we will be redirected to the home page of Amazon, then we will be opening our product page. If in case the items are out of stock or not available you will not be getting buy now option, this is where we will taking advantage. We will search for buy now element on the page, we keep refreshing until we find buy now. Once on clicking buy now it will direct you to address, mode of payment, review order page. These steps will be automated with selenium. At last, u are needed to enter your one-time password and confirm to order(in case of cash on delivery it's not needed)

Let's get into code:


  • Firstly you are required to install selenium and respective web drivers(i will be using chrome web drivers). Don’t know how to install it? here you go- installation documentation
  • Get your product link
  • add address and payment mode in prior

Now you are ready to get into the code!

Complete code is given at the end of a blog
Lets Import a few packages and will explain those while going through code

from selenium import webdriver
from import Options
from selenium.webdriver.common.keys import Keys
from import By
from import WebDriverWait
from import presence_of_element_located
Enter fullscreen mode Exit fullscreen mode

let us choose page load strategy to eager(which loads page before some images get loaded), which benefits in navigating quicker with pages and its data. Let us also create login_url variable which opens amazon sign-in page

options = Options()
options.page_load_strategy = 'eager'
login_url = ''
Enter fullscreen mode Exit fullscreen mode

Now let us create a function that takes your amazon account credentials and your products page link(or product share link).

Alt Text
XPath elements in the above code are used to identify input elements uniquely from the web page.

What is XPath?
XPath is a major element in the XSLT standard.
XPath can be used to navigate through elements and attributes in an XML document.

address_xpath is used to identify which address you want to deliver from your multiple pre-saved addresses in amazon. Chrome driver is used and set to eager loading mode so that the page doesn't wait for all images to be loaded

first, we open the login URL of amazon and log in using credentials provided from function parameters, all the browser actions are done with the help of the driver instance we created. once the page gets loaded, we redirect to another page with the help of driver.get(item_url) .till we find the item available we keep refreshing the page with the while provided. Presence of buy button is identified by driver.find_element_by_xpath(XPath-of-buy-button). once we find that element, we click using method. All the remaining code is to navigate through remaining pages and explained in the code with comments

Now let's call the function with username, password,item_url, CVV of our card in amazon

purchase("", "test", "", "123")
Enter fullscreen mode Exit fullscreen mode

By running the above code you will be able to automate the bot for purchasing products during a flash sale


In order to purchase a product from two different accounts at the same time, I used multiprocessing in python. For that, we need to import process from multiprocessing

from multiprocessing import Process
Enter fullscreen mode Exit fullscreen mode

Now create 2 processes and run at the same time using the below code, you can even more than 2 processes to buy from multiple accounts

if __name__ == '__main__':
    p1 = Process(target=purchase, args=["", "test_password", "product_link", "cvv"])
    p2 = Process(target=purchase, args=["", "test_password", "product_link", "cvv"])
Enter fullscreen mode Exit fullscreen mode

Woohoo it's over

Alt Text

Complete code :

First code is for a single person to purchase and the second code is for 2 persons(and can be modifiable for more people)to be purchased at the same time

Alt Text

Top comments (5)

nikenp16 profile image
Niken Patel • Edited

unable to use script in python 3.4
shows synta error while i copy paste in Python IDLE and hit the enter buttion

SyntaxError: multiple statements found while compiling a single statement

while i am able to execute single single line
plz tell what m i doing wrong ?

i am noob in this things
only want to use this in easiest way.

insaanimanav profile image

Hi I wanted to know how do you enter the OTP for the same ?

veerreshr profile image

At last , the script stops at OTP page , so u need to enter it manually and confirm

msaini8940 profile image
Mahesh saini

Can I use this script on auto buy any item from my Amazon account again again same product?

veerreshr profile image

Hello Mahesh, sorry for responding late. Might be script logic has been expired as it is more than 1 year and I wrote this script only for the purpose of learning automation tool and not intended for real use.