I call them Selenium Bots but they are more of mini bots that perform very simple tasks or automation using selenium and python as part of The Selenium Project I made to experiment and automate simple tasks.
The Selenium Project
When I started to learn and experiment python, I had a thought what if we can automate the way we browse the internet like some code to automatically sign in and follow, like or click a button and automate all of this process. Then I ran into Selenium bots and it’s just awesome, you must try this once!
Requirements
- Selenium [pip install selenium]
- Python 3+
- Chrome Web Driver [Download]
Remember, your Chrome Driver and Chrome Browser version must be same
Source code for all Selenium Bots I made
Alright, here we go. Just follow the flow from simple tasks to complex tasks
Note: All the below code is written in Python
Code to Open a Website
Simply opens a webpage for you,
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://matrixread.com/')
#opens the site in a new browser window
#this time mentioned is for the user to see
time.sleep(10)
#finally closes it
driver.quit()
#this is most siple way to start and test the use of selenium
Program to visit a Website N Times
This is the first program but repeats N times in a loop
from selenium import webdriver
import time
driver = webdriver.Chrome()
for i in range(50):
#loads this fifty times
print(i)
driver.get('https://matrixread.com/')
time.sleep(5)
driver.quit()
Collect and Print all links from a Page
from selenium import webdriver
driver = webdriver.Chrome()
#opens my Blog
driver.get('https://matrixread.com/')
#gather all the links in a site
for a in driver.find_elements_by_xpath('.//a'):
print(a.get_attribute('href'))
#prints all of them
Auto Login Bot
I made this for one of my college websites where we have to log in every day for attendance and there was no option to save or autofill our Login Credentials, you can easily customize and change this according to your needs
- We use the element id and Xpath to enter data and click
- Right-click in the username field
- Select Inspect element option
- In the HTML code look for the id of the button
id starts with a ‘ # ‘
Looks something like this #button-login
Sometimes the id method doesn’t work then repeat the same i.e right click and find the XPath of the element
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://sitetobeloggedin')
#opens the site in a new browser window
text_area = driver.find_element_by_id('username')
text_area.send_keys(yourusername)
text_area = driver.find_element_by_id('password')
text_area.send_keys(yourpassword)
# click submit button
submit_button = driver.find_elements_by_xpath('button-login')[0]
submit_button.click()
time.sleep(6)
driver.quit()
5th Selenium Bot
- A bot to open a website and click a button N times
- All my articles on Selenium Bots 🤖
This post is published as a part of my project called The Selenium Project, where I automate the boring stuff using python and selenium mostly. If you find it interesting check it out and drop a star at the GitHub Repository