DEV Community

Cover image for Simple Auto Login Bot
Muhammed Sabith
Muhammed Sabith

Posted on

Simple Auto Login Bot

A Simple Auto Login Bot

Things we need:

  1. Python
  2. Selenium
  3. A chrome webdriver

First we need to import the following things:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
Enter fullscreen mode Exit fullscreen mode
If you don't have selenium installed, go install it by entering this command :
pip install selenium
Enter fullscreen mode Exit fullscreen mode

Now We need to create a function, which accepts username, password, and a target url:

def startbot(username, password, url):

    path = "chromedriver.exe"
    service = Service(path)

    driver = webdriver.Chrome(service=service)

    driver.get(url)
    time.sleep(10)
    driver.close()
Enter fullscreen mode Exit fullscreen mode

what this code does is, it opens the chromedriver, goes to the target
url, waits for 10 second, and closes it

but what we want is a auto login bot, so it needs to find the input respective input fields. For this we are gonna use a test login page.

test_login_site

the test login page:

to find the input fields, we do the following thing:
    driver.find_element(By.NAME, "username").send_keys(username)
    driver.find_element(By.NAME, "password").send_keys(password)
    driver.find_element(By.ID, "submit").click()
Enter fullscreen mode Exit fullscreen mode
this finds the input fields by name, you can find the names given to input fields by inspecting the page, send_keys will set the username and password we've given. for the above test login the username and password are : student ** and **Password123

Now here's the complete code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def startbot(username, password, url):

    path = "chromedriver.exe"
    service = Service(path)

    driver = webdriver.Chrome(service=service)

    driver.get(url)

    driver.find_element(By.NAME, "username").send_keys(username)

    driver.find_element(By.NAME, "password").send_keys(password)


    driver.find_element(By.ID, "submit").click()

    print("successfully login")
    time.sleep(10)
    driver.close()


startbot("student", "Password123",
         "https://practicetestautomation.com/practice-test-login/")
Enter fullscreen mode Exit fullscreen mode

Now just run the python file and see the magic.

For installing chrome driver refer to the below page

install_chrome_driver








Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
developeraromal profile image
Aromal

Great work ๐Ÿ”ฅ

Collapse
 
masterdevsabith profile image
Muhammed Sabith

thanks