Login to a website automatically using Python

Hello programmers, in this tutorial we will learn how to log in to a website using Python.

This can be done with the help of the Selenium Python library.

Selenium: Selenium is a Python library tool used to automate web browsers and controlled by a program that can be coded.

Here we will see a demo of how to log in to a website using Python step by step.

Installation

  • Installation of selenium library
pip install selenium
  • Now we have to install the chromedriver for accessing the chrome webdriver through the selenium library.
  • path for chrome webdriver to pass into webdriver.
  • Now we have to select a website on which we want to log in so I’ll be using the GitHub login page to demonstrate to you how we can automatically log in using the Selenium python library.
  • URL=”https://github.com/login”
  • Now we can open the URL using the get() method.
  • # importing the webdriver from the selenium 
    from selenium import webdriver
    #path
    path='C:\\Users\\sumit\\.wdm\\drivers\\chromedriver\\win32\\102.0.5005.61\\chromedriver.exe'  
    # we use chrome as a webdriver
    driver = webdriver.Chrome(path)
      
    # URL
    url = "https://github.com/login"
      
    # Opening the URL
    driver.get(url)

    output:

Login to a website automatically using Python

  • Now we are on the GitHub login page and now we have to inspect the page to identify its HTML elements. To do this 1st you have to point your pointer to “username and email address”.
  • And then click right-click on your mouse and then go to “inspect” and then you see the HTML code shown below:

Login to github automatically using Python

  • Now to search or get the username/email address input field by id, we can see HTML code on the right and we can see id=”login_field” so this “login_field” help us to find the element by id.
  • similarly for password, id=”password”
  • And for sign-in, we can search the click button by name so we can see the HTML code for the sign-in button which is name=”commit”.
  • Now to pass the username, 1st we have to use the find_element() method to pass by id which is “login_field”.
  • And then we send our username with the help of the send_keys() method in that.
  • Similarly, for the password key, we have to pass the id as “password” in the find_element()┬ámethod.
  • And then pass the password in the send_keys method.
  • To click the button we search the button by name so with the help of find_element() we can pass the name-value which is “commit” and then simply use the click() method to click.

Login to GitHub Website automatically using Python

# importing the webdriver from the selenium 
from selenium import webdriver
#path 
path='C:\\Users\\sumit\\.wdm\\drivers\\chromedriver\\win32\\102.0.5005.61\\chromedriver.exe' 
# we use chrome as a webdriver 
driver = webdriver.Chrome(path) 
# URL 
url = "https://github.com/login" 
# Opening the URL
driver.get(url)
#your username and password to login into GitHub account
username="username"
password="password"
# finding username input field by find_element by id and pass username
driver.find_element{"id","login_field").send_keys(username)
# finding password input field by find_element by id and pass password
driver.find_element("id","password").send_keys(password)
# finding click button by find_element by name and click to login
driver.find_element("name","commit").click()

output:

login to a website automatically using Python

Thus, we have learned how to Login to a website automatically using Python.

Leave a Reply

Your email address will not be published.