Flash Sale magic
Veeresh
Posted on July 16, 2020
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.
Idea:
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:
Prerequisite:
- 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 selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
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 = 'https://amzn.to/38X1LHr'
Now let us create a function that takes your amazon account credentials and your products page link(or product share link).
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 elements.click() 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@test.com", "test", "itemurl.com", "123")
By running the above code you will be able to automate the bot for purchasing products during a flash sale
BONUS:
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
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=["test1@test.com", "test_password", "product_link", "cvv"])
p1.start()
p2 = Process(target=purchase, args=["test2@test.com", "test_password", "product_link", "cvv"])
p2.start()
p1.join()
p2.join()
Woohoo it's over
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
Posted on July 16, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.