DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Reproducible SadTalker Pipeline in Google Colab for Single-Image, Single-Audio Talking-Head Generation
  • Functional Programming Principles Powering Python’s itertools Module
  • Building and Deploying a Chatbot With Google Cloud Run and Dialogflow
  • Instant Integrations With API and Logic Automation

Trending

  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • The Pipeline Failed, But the Infrastructure Changed
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views
  • Shifting Bottleneck: How AI Is Reshaping the Software Development Lifecycle

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Automate Sign-In To Any Website Using Selenium Web-Driver

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.

By 
Varsha Sharma
·
Sep. 28, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Selenium 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.

Python
 
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. 

 

Python
 
#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 . 

Python
 
#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:

 


screenshot of amazon and inspected code


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.


Python
 
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: "[email protected]"

Enter in your password: 



screenshot of amazon.in and code on lefthand screen


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.

Element Python (language) Google Chrome Test script Driver (software) Google (verb) Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Reproducible SadTalker Pipeline in Google Colab for Single-Image, Single-Audio Talking-Head Generation
  • Functional Programming Principles Powering Python’s itertools Module
  • Building and Deploying a Chatbot With Google Cloud Run and Dialogflow
  • Instant Integrations With API and Logic Automation

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook