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:
- Access the GitHub website
- Enter login credentials
- 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”:
2. Finding “Password ID”:
3. 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:
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:
- Create your own ChatGPT with Python
- Bakery Management System in Python | Class 12 Project
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods
- Python Programming Examples | Fundamental Programs in Python
- Spell Checker in Python
- Portfolio Management System in Python
- Stickman Game in Python
- Contact Book project in Python
- Loan Management System Project in Python
- Cab Booking System in Python
- Brick Breaker Game in Python
- Tank game in Python
- GUI Piano in Python
- Ludo Game in Python
- Rock Paper Scissors Game in Python
- Snake and Ladder Game in Python
- Puzzle Game in Python
- Medical Store Management System Project in Python
- Creating Dino Game in Python
- Tic Tac Toe Game in Python
- Test Typing Speed using Python App
- MoviePy: Python Video Editing Library
- Scientific Calculator in Python
- GUI To-Do List App in Python Tkinter