You are on page 1of 9

Tutorial: Automating login to the Amazon website using

Selenium Webdriver

Table of Contents

1. Introduction
2. Verifying the Prerequisites
3. Steps for Automating the login to Amazon website using Selenium Webdriver

a. Create a Selenium Webdriver instance


b. Configure the driver of the web browser
c. Navigate to the web URL
d. Locating and performing action on the web elements

4. Entering the Username and Password in the login.py file


5. Executing the Automation Script
1. Introduction

Automating the login to the Amazon website using Selenium WebDriver is done by
creating an automation script and executing it. 

Selenium WebDriver is a cross-platform testing framework that allows you to execute


your tests on different browsers like Mozilla Firefox, Google Chrome, Internet Explorer,
Safari, and so on. It is a programming interface to create and run test cases. Selenium
WebDriver communicates with the browser using browser driver of a browser you are
using.
In this tutorial, you will use python as a programming language to create an automation
script and Chrome driver as a browser driver.
2. Verifying the Prerequisites
Ensure that the following prerequisites are met.

 Python 3.0 version is downloaded, installed, and running.


 Any Integrated Development Environment (IDE) is installed and configured.
 Selenium Java Client version is installed
 Chrome Driver is installed
Note: The procedure provided in this tutorial uses python as a programming language to create
an automation script and chrome driver as a browser driver.
3. Automating the login to the Amazon website using Selenium
Webdriver

The following are the components of the automation script to automate login to the Amazon
website using Selenium Webdriver.

a. Create a Selenium Webdriver instance


b. Configure the Driver of the Web browser
c. Navigate to the web URL
d. Locating and Performing action on the Web Elements

a. Create a Selenium Webdriver instance

You can configure the Webdriver instance depending on the web browser you are using to
access the Amazon website.
The following code snippet demonstrates the syntax for creating the Selenium web driver
instance.
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(r'<Path of the chrome driver >')
action = ActionChains(driver)

b. Configure the driver of the web browser

To configure the driver of the web browser:


a. Create a python file named automate_login.py
b. Copy the code snippet from “Create a Selenium Webdriver instance” section to the
xyz.py file
c. Enter the location where the chrome driver is installed in "Path of the chrome driver”
placeholder.

Note: Start-maximized is the function to maximize the web browser screen when the
automation script runs.
c. Navigating to the Amazon website
Pass the URL of the Amazon website to the driver.get method to navigate to the Amazon
website.

driver.get('http://www.amazon.in')

d. Locating and performing actions on the web element


Locators identify the elements that the test script interacts to replicate the user actions. In case
of Amazon.in, there is no Login button on the home page. You need to navigate to the Account
List menu and then you can access the Sign-in button.
Following is the sequence to login to the amazon:
1. Navigate to the Account List menu
2. Click on Sign-in option
3. Enter the username in Username textbox
4. Click on Continue button
5. Enter the password in Password textbox
6. Click on Submit button
.
The Action Chain class is used to automate the interactions like mouse movements, keypress,
hover over and drag and drop
Action Chains are implemented using action chain object which stores the actions in a queue.
When you call the methods from the action chain class, it performs the queued operations.
XPath is used to navigate the structure and elements of a webpage. XPath in Selenium helps to
find the elements that are not found by locators like ID, class, or name.
To find the xpath of the web element:
1. Right Click on the element
2. Click on Inspect
3. Click on Copy
4. Select Copy xpath
The following are the code snippets to perform the queued operations.

a. Code snippet to navigate to the Account List menu

firstLevelMenu = driver.find_element_by_xpath('//*[@id="nav-link-accountList"]/span[2]')
action.move_to_element(firstLevelMenu).perform()
time.sleep(3)

[@id="nav-link-accountList"] is the xpath of the Amazon Accounts List menu.

b. Code snippet to click on the Sign-in button


secondLevelMenu = driver.find_element_by_xpath('//*[@id="nav-flyout-ya-signin"]/a/span')
secondLevelMenu.click()
time.sleep(3)

[@id="nav-flyout-ya-signin"] is the xpath of the Amazon Sign-in button.

c. Code snippet to navigate the username textbox and enter the username in secured
manner

signinelement = driver.find_element_by_xpath('//*[@id="ap_email"]')
signinelement.send_keys(logindata.USERNAME)
time.sleep(3)
[@id="ap_email"] is the xpath of the Amazon Username textbox.

d. Code snippet to click on the continue button

cont = driver.find_element_by_xpath('//*[@id="continue"]')
cont.click()
time.sleep(3)
[@id="continue"] is the xpath of the Amazon Continue button.

e. Code snippet to navigate the password textbox and enter the password in secured
manner
passwordelement = driver.find_element_by_xpath('//*[@id="ap_password"]')
passwordelement.send_keys(logindata.PASSWORD)
time.sleep(3)
[@id="ap_password"] is the xpath of the Amazon Password textbox.

f. Code Snippet to Click on Submit button

login = driver.find_element_by_xpath('//*[@id="signInSubmit"]')
login.click()
time.sleep(3)

[@id="signInSubmit"] is the xpath of the Amazon Continue button.


4. Entering the Username and Password in login.py file

You can create a logindata.py file to store the username and password for accessing the
Amazon website. This separates and securely stores the username and password values from
the automation script.

The following code snippet demonstrates how to store Username and Password in
logindata.py file
USERNAME = 'USERNAME'
PASSWORD = 'PASSWORD'
5. Executing the Automation Script

On executing the automation script, Selenium will navigate to the Chrome browser and open
Amazon login page. Then, it will log in using the credentials entered in the login.py file.

You might also like