How To Automate Login using Selenium in Python 2022

Automate Login using Selenium in Python in 2022

Introduction

Before learning How To Automate Login using Selenium in Python, let’s see what is automatic login? Many third-party libraries are supported by the Python programming language for automating tasks. Selenium is a popular third-party functional testing library for Python. It is frequently used for automating and testing web browsers.

By executing a Python script, we may automate the browser(s) installed on our system and automate numerous operations such as login, open pages, and search using the browser.

The Selenium web drivers work with the four most common web browsers: Chrome, Firefox, Edge, and Safari. This implies that we can control and automate these web browsers using a single Python module, Selenium. Logging onto a website automatically is a pretty neat and useful skill in automation.

In this post, we will create a Python program that will Automate Login using Selenium and automatically do the following 3 tasks:

  1. Access the GitHub website
  2. Enter login credentials
  3. Sign in to GitHub

6 steps to Automate Login using Selenium in Python:

Step.1: Importing the Libraries

Selenium isn’t a typical Python library. As a result, you must first install it in your Python environment before you can use it. Use the following command (through the command prompt or terminal) to install the Selenium library:

pip install selenium

You’ll also need to import certain special Selenium modules from Python’s library. At the very least, you must perform the following:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

Also, you can try alternate libraries not just Automate Login using Selenium. Click here to check other libraries.

Step.2: Install the Web Driver

Because Selenium will automate the login via a web browser, we will need the web driver to interface with the web browser and automate the procedure. Because most developers use it, we will download and utilize the Chrome web driver for this tutorial. You may alternatively choose another web driver from the list below if you prefer:

Chrome Driver: https://chromedriver.chromium.org/home

Edge web driver: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Firefox web driver: https://github.com/mozilla/geckodriver/releases

Safari web driver: https://webkit.org/blog/6900/webdriver-support-in-safari-10/

After importing the modules, we will enable the newly installed Chrome driver.

# initialize the Chrome driver
driver = webdriver.Chrome("D:/chromedriver")

Step.3: Create Variables for Login Credentials

# Github credentials
username = "CopyAssignment.com"
password = "Welcome"

Step.4: Launch the Browser and Open the URL

# head to github login page
driver.get("https://github.com/login")

As you can see, we gave the chrome driver chromedriver.exe’s absolute path. This is a must-required process to Automate Login using Selenium in Python. This will launch the Chrome browser using our Python script. Using the driver.get() function and the initialized driver, we can access github.com. This will take us to the website’s login page.

Step.5:  Enter Login Credentials and Sign In

# find username/email field and send the username itself to the input field
driver.find_element(By.ID,'login_field').send_keys(username)

# find password input field and insert password as well
driver.find_element(By.ID,'password').send_keys(password)

# click login button
driver.find_element(By.NAME,'commit').click()

As we all know, when we want to enter into a website, we must first fill out the HTML login form. As a result, when we visit GitHub, we must fill out the login form, including the Username(or Email) and password boxes.

However, if we want to automate this process with Selenium, we must tell it which input fields to populate. To do so, we must first get the Username(or Email) and password entry areas. We will also have access to the login button since once we have completed the fields, we will need to click it in order to log in.

Here, we use Selenium to open the site of our need (in this example, Gmail) and examine items across the email box, password box, and Next button to retrieve their ID and NAME.

  • Using find_elemen(By.ID,”id”) : We can locate the needed element using the functionality made available by the selenium module (username ID, password ID)
  • Using find_elemen(By.NAME,” name”): function offered by the Selenium module, we are able to locate the necessary piece (Name of submitting Button)
  • Using find_elements(By.CLASS_NAME, “ClassName”): Use this when you want to locate an element by class name.
  • Using send_keys(): We will transmit the data into the box using the function that the selenium module offers.
  • Using click(): We do the selenium module’s function by clicking on the active element.
  • get(): enables us to access a webpage.

1. Finding “Login Field”:

fetching login field id

2. Finding “Password ID”:

finding password block id

3. Finding Submit Button Name:

finding submit button name

Step.6: Verify Login Status

# wait the ready state to be complete
WebDriverWait(driver=driver, timeout=10).until(
    lambda x: x.execute_script("return document.readyState === 'complete'")
)
error_message = "Incorrect username or password."

# get the errors (if there are)
errors = driver.find_elements(By.CLASS_NAME,"flash-error")

# if we find that error message within errors, then login is failed
if any(error_message in e.text for e in errors):
    print("[!] Login failed")
else:
    print("[+] Login successful")

The preceding code will attempt to identify the “Home” button displayed when you initially log in to GitHub for a maximum of 10 seconds. Again, there are other approaches to this, such as waiting for an element to be clickable, visible, or present on the page. If the username and password are accurate, the message “Login Successful” appears; otherwise, it reads “Login Failed.”

Complete Code to Automate Login using Selenium in Python

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
 
# Github credentials
username = "CopyAssignment.com"
password = "Welcome"
 
# initialize the Chrome driver
driver = webdriver.Chrome("D:/chromedriver")
 
# head to github login page
driver.get("https://github.com/login")
# find username/email field and send the username itself to the input field
driver.find_element(By.ID,'login_field').send_keys(username)
# find password input field and insert password as well
driver.find_element(By.ID,'password').send_keys(password)
# click login button
driver.find_element(By.NAME,'commit').click()
 
# wait the ready state to be complete
WebDriverWait(driver=driver, timeout=10).until(
    lambda x: x.execute_script("return document.readyState === 'complete'")
)
error_message = "Incorrect username or password."
# get the errors (if there are)
errors = driver.find_elements(By.CLASS_NAME,"flash-error")
# if we find that error message within errors, then login is failed
if any(error_message in e.text for e in errors):
    print("[!] Login failed")
else:
    print("[+] Login successful")  

Output:

Final output screenshot to Automate Login using Selenium in Python

That’s it, this is supposed to serve as a fundamental starting point to Automate Login using Selenium in Python. Many thanks for reading our articles. Please leave your thoughts or suggestions in the space provided below.


Also Read:

Share:

Author: Ayush Purawr