AI & Python #15: Automate The Most Tedious Task on The Internet With Python
Automate filling in web forms with Python and Selenium.
Hi! Iâve just emailed the 20 lucky winners of the giveaway 34k subs. Check out if you got an email from articial corner today (besides this one). Thanks all for participanting!
Nowadays everyone wants to get some data from you, so to create a new account, participate in a giveaway, or get a free e-book you need to fill in your information in a form.
This isnât a big deal if you do this only once, but things get tedious when you have to repeat the same steps over and over again, so the best solution is to automate it with Python.
Letâs automate filling in web forms with Python. First, weâll see how to fill in a form with data we create in a Python list, and then Iâll show you how to generate fake data to fill in multiple forms.
You can follow this tutorial by watching my YouTube video (this project starts at 31:23 on the video) or following the guide below.
How to Automate Filling in Web Forms with Python
To automate web forms with Python, weâre going to use a library called Selenium. Follow the instructions below to install it.
1. Install selenium
To install Selenium, open up a terminal and run the following command.
pip install selenium
That will install Selenium 4, which is the version weâre going to use for this tutorial.
Then we need to download chromedriver:
Check your Google Chrome version (on Chrome click on three dots, click on âhelpâ and then select âabout Google Chromeâ)
Download the right Chromedriver here (after any Chrome update you need to download the Chromedriver file again)
Unzip the driver and copy the path where youâre leaving the Chromedriver file.
Filling in Google Forms
Letâs import the libraries weâll use for this automation.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
Then we create a path
where our chromedriver file is located, the website
weâre going to automate and a driver
that will helps us interact with the website through Selenium.
Website: For this simple demonstration, weâre going to use a basic template called âContact Informationâ from Google Form that you can find here. Copy the link.
In case the link stops working, go to Google Forms and select the template below to generate the form and get your own link.
Note: If you create your own form, make sure you turn off the âLimit to 1 responseâ option inside Settings, so we can fill in as many forms as we want.
Here are the variables we create to start working with Selenium.
path = '/Users/.../chromedriver' # paste your path here
service = Service(executable_path=path)
website = 'https://forms.gle/GRgxTrG8FfXUCLE99' # paste your link
driver = webdriver.Chrome(service=service)
After this, we open the website with Selenium by using the driver.get()
driver.get(website)
Also, we add a wait, so we let the website load all the information. To do so, we import the time
module.
import time
time.sleep(3)
At this point, the code should look like this:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import pandas as pd
path = '/Users/.../chromedriver' # your path
service = Service(executable_path=path)
website = # your link
driver = webdriver.Chrome(service=service)
driver.get(website)
time.sleep(3)
Now to fill in information with Selenium we need to inspect the website. Letâs inspect the âNameâ field. To do so, right-click on any blank section in the first block and click on âInspectâ.
Developer tools will open with the HTML document of the website.
Regardless of what element is highlighted on your computer, weâre going to use the one that has the data-params
attribute name because inside itâs the name of the field (e.g., Name, Email, Address, Phone number, Comments).
Now letâs create the XPath of the âNameâ field. Hereâs the syntax of an XPath:
In addition to that, we also use the contains()
function, so our XPath will be:
//div[contains(@data-params, "Name")]
Now we have to inspect the field where the placeholder âYour answerâ is located.
Unlike most websites, Google Form follows an inconsistent pattern some fields like âEmailâ has the <input>
tag, while other fields like âAddressâ has the <textarea>
tag.
The final XPath of the field with the âYour answerâ placeholder will vary for those with the <input>
and <textarea>
tag.
//div[contains(@data-params, "Name")]//textarea //div[contains(@data-params, "Name")]//input
To put them together we use the âorâ conditional |
//div[contains(@data-params, "Name")]//textarea | //div[contains(@data-params, "Name")]//input
Now we use .find_element
and .send_keys
to fill in data (copy/paste the code to your code editor in case the code below isnât readable on this page)
text_input = driver.find_element(by='xpath',
value='//div[contains(@data-params, "Name")]//textarea | '
'//div[contains(@data-params, "Name")]//input')
text_input.send_keys("Write your text here")
Create a list with data and fill in all the fields
There are different Python libraries like faker
that can help us generate fake data for us (Iâll show you how faker
works at the end), but to keep this simple, letâs generate data in a list ourselves and then put them into a dictionary.
fields = ['Name', 'Email', 'Address', 'Phone number', 'Comments']
data = ['Frank', 'frank@example.com', '123 St', '987654321', 'Hello World']
my_form = dict(zip(fields, data))
Now to fill this data in all the fields of our form we put the .find_element
and .send_keys
inside a for loop (copy/paste the code to your code editor in case the code below isnât readable on this page)
for field, data in my_form.items():
text_input = driver.find_element(by='xpath',
value=f'//div[contains(@data-params, "{field}")]//textarea | '
f'//div[contains(@data-params, "{field}")]//input')
text_input.send_keys(data)
Submit the form
Finally, we have to click on the âSubmitâ button.
To click on âSubmitâ with Selenium, we inspect the button to create its XPath and then use the .click
method to click on it.
submit_button = driver.find_element(by='xpath',
value='//div[@role="button"]//span[text()="Submit"]')
submit_button.click()
Note: If you have your browser in another language, probably the value inside the text()
attribute would be in that language. For example, my browser is in Spanish so I have to change part of the XPath to //span[text()=âEnviarâ]
.
Hereâs the code weâve written so far.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
path = '/Users/.../chromedriver' # your path goes here
service = Service(executable_path=path)
website = # your link goes here
driver = webdriver.Chrome(service=service)
fields = ['Name', 'Email', 'Address', 'Phone number', 'Comments']
data = ['Frank', 'frank@example.com', '123 St', '987654321', 'Hello World']
my_form = dict(zip(fields, data))
driver.get(website)
time.sleep(3)
for field, data in my_form.items():
text_input = driver.find_element(by='xpath',
value=f'//div[contains(@data-params, "{field}")]//textarea | '
f'//div[contains(@data-params, "{field}")]//input')
text_input.send_keys(data)
submit_button = driver.find_element(by='xpath', value='//div[@role="button"]//span[text()="Submit"]')
submit_button.click()
time.sleep(1)
driver.quit()
Congratulations! Now you know how to fill in web forms. Now you can take this to the next level by sending hundreds of forms with fake data.
Create fake data and submit hundreds of forms
Now that you know the basics you can jump to the minute 44:52 of my video below to learn how to generate fake data and submit hundreds of forms with Python.