Skip to content
Related Articles

Related Articles

Improve Article

How to Build a Simple Auto-Login Bot with Python

  • Difficulty Level : Medium
  • Last Updated : 15 Sep, 2021

In this article, we are going to see how to built a simple auto-login bot using python.

In this present scenario, every website uses authentication and we have to log in by entering proper credentials. But sometimes it becomes very hectic to login again and again to a particular website. So, to come out of this problem lets, built our own auto login bot using python.

We will be using Selenium (python library) for making the auto-login bot. Python Selenium library helps us to access all functionalities of Selenium WebDriver like Firefox, Chrome, Remote etc.

Installation

First of all, we have to install selenium using the below command:

pip install selenium

After successful installation of selenium, we also have to install chromedriver for accessing the chrome webdriver of selenium. You can download the same from here (Download version according to your system chrome version and according to your OS).



Make sure that you have noted the location where the chromedriver has been downloaded (as it is used in our python script). Now After downloading extract the zip file and please note the file location of the extracted file as we have needed it later in python code. (You can find the location by clicking on properties and then details).

Stepwise Implementation:

  • First of all import the webdrivers from the selenium library.
  • Find the URL of the login page to which you want to logged in.
  • Provide the location executable chrome driver to selenium webdriver to access the chrome browser.
  • Finally, find the name or id or class or CSS selector of username and password by right-clicking inspect on username and password.

Below is the implementation:

Python3




# Used to import the webdriver from selenium
from selenium import webdriver 
import os
 
# Get the path of chromedriver which you have install
 
def startBot(username, password, url):
    path = "C:\\Users\\hp\\Downloads\\chromedriver"
     
    # giving the path of chromedriver to selenium websriver
    driver = webdriver.Chrome(path)
     
    # opening the website  in chrome.
    driver.get(url)
     
    # find the id or name or class of
    # username by inspecting on username input
    driver.find_element_by_name(
        "id/class/name of username").send_keys(username)
     
    # find the password by inspecting on password input
    driver.find_element_by_name(
        "id/class/name of password").send_keys(password)
     
    # click on submit
    driver.find_element_by_css_selector(
        "id/class/name/css selector of login button").click()
 
 
# Driver Code
# Enter below your login credentials
username = "Enter your username"
password = "Enter your password"
 
# URL of the login page of site
# which you want to automate login.
url = "Enter the URL of login page of website"
 
# Call the function
startBot(username, password, url)


Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :