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.
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.')
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.
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/')
#set a ten second delay to allow the web page to load
time.sleep(10)
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.
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.
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')
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.')
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.
If you like this content, subscribe to our email list in order to get notified about new post.