Automate Sign-In To Any Website Using Selenium Web-Driver
Selenium web-driver can be used to automate the Amazon.in sign-in process. Discover how to set it up with this tutorial.
Join the DZone community and get the full member experience.
Join For FreeSelenium web-driver can be used to automate the Amazon.in sign-in process. Here is a brief overview of this process:
Go to Amazon.in's home page, http://www.amazon.in/
Hover cursor over the "Account & Lists" block, and click the sign-in button.
Once you are on the sign-in page, enter the username, password into the appropriate text boxes.
Select the Sign In button.
In the next part of this article, we'll be covering how to automate the above process.
Environment Prerequisites:
Install Selenium: In this case, we used version 3.141.0.
Chromedriver: Download the Driver as per Supported Chrome Browser version. The demo in this article uses driver version 89.0.4389.23.
VSCode: IDEs of any type can be used.
Operating System: This implementation uses RHEL 7 (3.10.0-957.27.2.el7.x86_64).
Install Selenium
The following commands will install Selenium for you:
$ pip install selenium
You can check your installation by running the following command:
$ pip list
Python Script to Automate the Process
Save a file autologin.py with below content.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from getpass import getpass
username = input("Enter in your username: ")
password = getpass("Enter in your password: ")
driver = webdriver.Chrome("/home/vasharma/Downloads/WebDrivers/chromedriver")
driver.get("https://www.amazon.in/")
SignIn_button = driver.find_element_by_xpath('//*[@id="nav-link-accountList"]/span')
SignIn_button.click()
username_textbox = driver.find_element_by_id("ap_email")
username_textbox.send_keys(username)
Continue_button = driver.find_element_by_id("continue")
Continue_button.submit()
password_textbox = driver.find_element_by_id("ap_password")
password_textbox.send_keys(password)
SignIn_button = driver.find_element_by_id("auth-signin-button-announce")
SignIn_button.submit()
Here's a detailed breakdown of each step.
Step 1: Initialize the WebDriver
Selenium WebDriver is a browser-controlling library. It is designed to support all major browsers (Firefox, Edge, Chrome, Safari, Opera, etc) and is available to programmers in several languages. In this tutorial, we will be using Google Chrome browser to automate login to amazon using python.
Action Chain performs a number of operations such as click, right-click, etc.
Here this script (autologin.py) first opens www.amazon.in and then mouse hovers over Account & Lists, clicks on Sign-in, which then redirects the browser to sign-in page automatically.
#The first step is to import the web driver with the following statement
from selenium import webdriver
#ActionChains are for doing actions like mouse hover over and drag and drop.
from selenium.webdriver.common.action_chains import ActionChains
# Prompt the user for a password without echoing
from getpass import getpass
Step 2: Initialize the Google Chrome Browser
After you have downloaded and unzipped the driver for your OS, place it in a known location so that it can be passed to the webdriver.Chrome() class.
In my case, chromedriver is located under "/home/vasharma/Downloads/WebDrivers/chromedriver"
Since we're interested in automating Amazon login, we'll pass the desired URL to open in the browser .
#Configure the Google Chrome browser
driver = webdriver.Chrome("/home/vasharma/Downloads/WebDrivers/chromedriver")
# head to amazon login page
driver.get("https://www.amazon.in/")
Step 3: Inspect the Page to Identify Its HTML Elements
The locators from a web page are required for any automated action. These are unique identifiers associated with the web elements such as text, buttons, tables, div, etc.
It is not possible to interact with the web page if the test script is not able to find the web elements. Selenium Webdriver provides several techniques for locating the web elements.
Navigate to Amazon login page and inspect the page to identify its elements:
Here the id of the email and password input fields, and the xpath of the Sign in button will be useful for us to retrieve these elements in code and insert them programmatically.
Notice the email address input field has ap_email as id, similarly the password input field has the id of ap_password.
For the sign-in button under Account & Lists we will use driver.find_element_by_xpath.
Finally for the last Sign-In button after entering the username/password locator is driver.find_element_by_id.
SignIn_button = driver.find_element_by_xpath('//*[@id="nav-link-accountList"]/span')
SignIn_button.click()
username_textbox = driver.find_element_by_id("ap_email")
username_textbox.send_keys(username)
Continue_button = driver.find_element_by_id("continue")
Continue_button.submit()
password_textbox = driver.find_element_by_id("ap_password")
password_textbox.send_keys(password)
SignIn_button = driver.find_element_by_id("auth-signin-button-announce")
SignIn_button.submit()
Execute the script
From the terminal, run the script. It will prompt you for your credentials.
Upon entering the credentials, the user is logged in via Chrome to Amazon.
$ python ./autologin.py
Enter in your username: "example@gmail.com"
Enter in your password:
We've successfully automated sign-in to Amazon using Selenium!
To log in to any website, you can customize this script accordingly by adding the necessary page elements.
Opinions expressed by DZone contributors are their own.
Comments