A Simple Auto Login Bot
Things we need:
- Python
- Selenium
- 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
If you don't have selenium installed, go install it by entering this command :
pip install selenium
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()
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.
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()
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/")
Now just run the python file and see the magic.
For installing chrome driver refer to the below page
Top comments (2)
Great work ๐ฅ
thanks