Building a python bot to fill out online forms

I’ve been working with selenium lately and I wanted to share the cool things you can do when you combine its webdriver API and a bit of Python programming. So I’m going to show you how to build a python bot to open a web browser, visit a web page, and fill out its online form.

Code

				
					from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import time

driver = webdriver.Firefox()
driver.get('https://atypicallawncare.com/basic-lawn-care-martinez-evans-augusta/')

time.sleep(15)

name = driver.find_element(By.ID, 'form-field-name')
phone_number = driver.find_element(By.ID, 'form-field-field_8362fdb')
email = driver.find_element(By.ID, 'form-field-email')
address = driver.find_element(By.ID, 'form-field-field_9ebdcba')
service_type = driver.find_element(By.ID, 'form-field-field_8688920')
special_instructions = driver.find_element(By.ID, 'form-field-message')

name.send_keys('Bot Botting')
phone_number.send_keys('(706) 867-5309')
email.send_keys('thisisnotarealemail@fakedomain.com')
address.send_keys('42 Wallaby Way, Sydney')
Select(service_type).select_by_visible_text('Weekly with hedges')
special_instructions.send_keys('Please do not take this form submission as a real customer. A bot is typed this.')


				
			

Getting started

First things first we need to open up Sublime text and create a new file. Once this file has been created. Save it as “main.py” within a dedicated folder but keep it open to start writing code.

Example new file
Example of new file in dedicated folder

Install dependent modules

				
					from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import time
				
			

Establish a driver object.

				
					driver = webdriver.Firefox()
				
			

Get the website you're targeting

				
					driver.get('https://atypicallawncare.com/basic-lawn-care-martinez-evans-augusta/')
#set a ten second delay to allow the web page to load
time.sleep(10)
				
			

Verify everything is working right

Run main.py in sublime. Ensure a Firefox browser has launched and the webpage you are targeted is retrieved. In my example, a successful build results in the following.

Example of successful webpage retrieval

Target the elements you are going to send information to

After verifying you can use selenium to retrieve a webpage, you need to target the elements you’re going to have your program interact with. 

In our example, we have 6 form field options that we’ll be interacting with.

  1. Name
  2. Phone Number
  3. Email
  4. Address
  5. Service Type?
  6. Have any special instructions for us?

 

Create objects for your targets

Selenium has various ways of interacting with elements on a webpage. Here is a list of them. For our example we are going to target element IDs.

				
					name = driver.find_element(By.ID, 'form-field-name')
phone_number = driver.find_element(By.ID, 'form-field-field_8362fdb')
email = driver.find_element(By.ID, 'form-field-email')
address = driver.find_element(By.ID, 'form-field-field_9ebdcba')
service_type = driver.find_element(By.ID, 'form-field-field_8688920')
special_instructions = driver.find_element(By.ID, 'form-field-message')

				
			

Interact with the objects you created by sending keys to them

				
					name.send_keys('Bot Botting')
phone_number.send_keys('(706) 867-5309')
email.send_keys('thisisnotarealemail@fakedomain.com')
address.send_keys('42 Wallaby Way, Sydney')
Select(service_type).select_by_visible_text('Weekly with hedges')
special_instructions.send_keys('Please do not take this form submission as a real customer. A bot is typed this.')
				
			

Build your bot and watch it work

If you followed these steps, you have now programmatically visited a website and filled out its form field. Please note, that I own atypicallawncare.com and that you should never run bots maliciously on websites that you do not own.