How To Fill Form Fields Using Python And Selenium

How To Fill Form Fields Using Python And Selenium

In this article, I will show you two methods you can use to fill forms using Selenium. The first method I will show you is a pretty popular one, but I was working on a script some time back and for some reason this method did not fill the form field I wanted it to fill, but the second method I am going to show you worked like magic. I will show you the first method first before showing the second method. With that said, let's cut to the chase.

I am going to start by creating a virtual environment for this project by running python -m venv seleniumformfiller on the command line. After that is done, I will install Selenium and webdriver-manager by running pip install selenium webdriver-manager on the command line, in the same folder I created my virtual environment.

I will be using python's homepage to demonstrate method 1; you can use this knowledge on any webpage that has a form to be filled.

Method 1

In your python file, enter the code snippet below:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
url = 'https://www.python.org/'
options = Options() /* Initialize the options class which is one of the arguments that will be used in webdriver.Chrome class */
options.headless = False /* This is set to false so we can see how the script interacts with the browser via the browser UI */
browser = webdriver.Chrome(ChromeDriverManager().install(), options=options) /* class that install chromedriver. ChromeDriverManager().install() ensures the latest version of the chrome driver is installed. */
browser.maximize_window() /* This maximizes the browser window to full screen. */browser.get(url)
element = browser.find_element_by_name('q') /* This snippet fills the search form on python.org */
element.clear()
element.send_keys("Pycon") /* This method is used to fill the form, the form will be filled with the value passed into the method*/
element.send_keys(Keys.RETURN) /* Keys.RETURN is used to simulate a user pressing the "enter" key as though they are done filling the form and they want to submit it.*/
time.sleep(3)
browser.close()

If I run the script, it navigates to the url provided and fills the form. Because filling the form is all the script is doing, it will stop executing once it fills the form and sends the RETURN key. You might want to remove time.sleep() function if you will be performing more than just filling a form. I added it because the browser closes almost immediately the form is filled.

Method 2

I will be using https://www.edmunds.com/cars-for-sale-by-owner/ to demonstrate this method.

This was the webpage that has the form that I said the first method did not work for.

Copy the code snippet below:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

url = 'https://www.edmunds.com/cars-for-sale-by-owner/'

options = Options()
options.headless = False 

browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)
browser.maximize_window()
browser.get(url)
browser.set_page_load_timeout(45) /* If the page does not load within the value passed into the function, it returns a timeout error. */

'''
This is to ensure that the zipcode input field has been loaded and can be clicked before we attempt to find the input field. We are getting the input field because we need to pass in our own zipcode.
'''
WebDriverWait(browser, 15).until(EC.element_to_be_clickable((By.NAME, 'zip')))
zipcode_form = browser.find_element_by_name('zip') /* this finds the zipcode input field. */

'''  
ActionChains is used to perform some low-level interaction with the webpage. Such interactions include hover, mouse movements, key press, clicks. It can also be used to fill in a form.
In our case we want to clear the default zipcode in the zipcode input field and then use the zipcode entered by the user.
'''
action = ActionChains(browser) 
action.move_to_element(zipcode_form).click(zipcode_form).click(zipcode_form).send_keys(Keys.DELETE).send_keys(str(85001)).perform()

time.sleep(3)
browser.close()

Running this script, the browser will open up and the zipcode input field will be filled.

Links to documentation of libraries used in this article:

Selenium

Webdriver-manager

You can follow me on github here