Professional Documents
Culture Documents
Selenium Webdriver
Table of Contents
1. Introduction
2. Verifying the Prerequisites
3. Steps for Automating the login to Amazon website using Selenium Webdriver
Automating the login to the Amazon website using Selenium WebDriver is done by
creating an automation script and executing it.
The following are the components of the automation script to automate login to the Amazon
website using Selenium Webdriver.
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)
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')
firstLevelMenu = driver.find_element_by_xpath('//*[@id="nav-link-accountList"]/span[2]')
action.move_to_element(firstLevelMenu).perform()
time.sleep(3)
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.
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.
login = driver.find_element_by_xpath('//*[@id="signInSubmit"]')
login.click()
time.sleep(3)
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.