One of my friends posted this in our Google Classroom:

Google Docs

It was quite obvious it was meant to be a joke and not meant to be taken seriously. Having some free time then, I decided to have a little fun.

I answered the Google Doc ... 24000 times.

Whether it's repetitive web-based administration tasks or crawling the web to read stock data (which I recently finished making) or fun little pranks on our classmates, interacting with websites by entering forms or reading data from websites can be automated.

Selenium

Selenium is a great, free tool for automatically interacting with websites.

It has the ability to:

  1. Click on buttons and links
  2. Enter text in form fields
  3. Read text
  4. Find HTML elements
  5. And a lot more

Selenium Setup

The web browser we would use is Firefox. I prefer Chrome, but there’s some weird stuff with Chrome so I didn’t use it.

In order to use Firefox, install geckodriver and add geckodriver to the PATH (windows):

Windows PATH Change

Install Selenium as so (for Python):

pip install -U selenium

Tutorial

In order to identify elements to interact with, we use the element's XPath because while it may lack a class or id, every element has an unique XPath.

We can find an element’s XPath by using ‘inspect element’ and clicking ‘Copy XPath’ as so:

Copy XPATH from Website

Sample Code

The code below is able to interact with this form, read the password, enter it in the textbox, click the "Not Secure" button, and click submit after 5 seconds.

My password is SecretPassword1#

The password:

How secure is the password?

Secure

Not Secure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import time
from selenium import webdriver

url = "https://codeodysseys.com/posts/website_automation/"
# replace path with the folder where you keep geckodriver.exe
path = "C:\\Users\\panda\\Desktop\\Python\\Selenium" 

browser = webdriver.Firefox(path)
browser.get(url)
time.sleep(1) # wait for the page to load

password = browser.find_element_by_xpath('/html/body/div[4]/div/div/form/p[1]/span[2]').text
print(password)

# enter password in field
browser.find_element_by_xpath('/html/body/div[4]/div/div/form/p[2]/input').send_keys(password) 
# click "not secure" button
browser.find_element_by_xpath('/html/body/div[4]/div/div/form/input[2]').click() 

time.sleep(5) # wait a few seconds before submitting
# click submit
browser.find_element_by_xpath('/html/body/div[4]/div/div/form/button').click() 
browser.close()

Please do not spam Google Docs. I included it as a fun anecdote and was given permission.