One of my friends posted this in our Google Classroom:
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 is a great, free tool for automatically interacting with websites.
It has the ability to:
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):
Install Selenium as so (for Python):
pip install -U selenium
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:
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.
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/div/div/form/p/span').text print(password) # enter password in field browser.find_element_by_xpath('/html/body/div/div/div/form/p/input').send_keys(password) # click "not secure" button browser.find_element_by_xpath('/html/body/div/div/div/form/input').click() time.sleep(5) # wait a few seconds before submitting # click submit browser.find_element_by_xpath('/html/body/div/div/div/form/button').click() browser.close()
Please do not spam Google Docs. I included it as a fun anecdote and was given permission.