Login to a Website Using Selenium Python

Login to a Website Using Selenium Python

Sometimes we may want to automate tasks in a browser, like a login to a website. It can be helpful in scenarios where we want to scrape data from websites that require login.

Python offers many packages for web automation, such as Selenium.

Login to a Website Using Selenium Python

Selenium is a powerful automation and testing tool for web browsers. We write scripts that take control of the web browser and perform specific actions.

Install Selenium and Chrome Webdriver

To install Selenium, we use the following command.

#Python 3.x
pip install selenium

ChromeDriver is another executable that Selenium WebDriver uses to interact with Chrome. If we want to automate tasks on the Chrome web browser, we also need to install ChromeDriver.

Based on the version of the Chrome browser, we need to select a compatible driver for it. Following are the steps to install and configure the Chrome driver:

  1. Click on this link. Download Chrome driver according to the version of your Chrome browser and the type of operating system.
  2. If you want to find the version of your Chrome browser, click on the three dots on the top right corner of Chrome, click on Help, and select About Google Chrome. You can see the Chrome version in the About section.
  3. Extract the zip file and run the Chrome driver.

Code for Automated Login to a Website Using Selenium Python

We created the WebDriver instance in the following code and specified the path to the Chrome driver. Then we have specified the URL of the target website to the driver instance. It will open the target website in the desired browser.

We have stored dummy Email and password in the variables that we will later use to sign in. To automate typing the email and password and clicking the login button, we have to find the name/id (which one is available) of those web elements.

We have to open the desired website in the browser, right-click on the element, and click Inspect element. Here, we can see the HTML code, including the element’s name.

We will copy the element’s name and pass it as an argument to the find_element_by_name() method with the driver’s instance. We will repeat the process for the remaining two elements.

This method will return the web element, which we will store in variables. We will automate typing the email and password in their relevant fields’ using the sendkeys() method.

We set a timer of two seconds after filling the fields so that the user can observe what is happening through a delay in the process. Finally, the login button will automatically click through the click() method.

Example code:

#Python 3.x
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(r"E:\download\chromedriver.exe")
email ="myemail@email.com"
password= "mypassword1@23"
email_textfield = driver.find_element_by_name("email")
password_textfield = driver.find_element_by_name("password")
login_button = driver.find_element_by_name("LoginDAPLoginForm")


Selenium Login

Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.


Related Article - Python Selenium

  • Check if Element Exists Using Selenium Python
  • Python Selenium Refresh Page
  • WebDriverException: Message: Geckodriver Executable Needs to Be in PATH Error in Python
  • Scroll Down Web Page Using Selenium in Python
  • Install Python Selenium in macOS