githubEdit

Extract and Fill Web Forms Automatically Using Gaffa

Web forms are some of the most common and repetitive elements that users often interact with as developers. Whether you are collecting data, testing user flows, or even building other automation systems.

In this guide, you'll learn how to use pase_json action to extract the structure of a web form and then automatically fill and submit it using Gaffa's browser automation features.

By the end of this guide, you will be able to:

  • Extract structured form data (labels, input names, required fields, and placeholders) using parse_json

  • Define and use schemas to reliably understand page structure

  • Build a simple interactive CLI that collects user input

  • Automatically fill and submit a web form using Gaffa browser actions

Prerequistes

  1. Install Python 3.10 or newer.

  2. Create a virtual environment

python -m venv venv && source venv/bin/activate
  1. Install the required libraries

pip install requests openai
  1. Get your Gaffa APIarrow-up-right key and store it as an environment variable:

GAFFA_API_KEY=your_gaffa_api_key
  1. Install the required library

pip install requests

What You'll Build

In this tutorial, you'll create a Python script that:

  • Extracts form fields - Uses Parse JSON to analyze any web form and identify all input fields.

  • Collects user input - Prompts the user in the terminal to provide values for each field.

  • Submits the form - Automatically fills and submits the form using Gaffa's browser automation.

By the end, you'll have a working form automation tool that can be adapted for countless use cases.

Set Up Your Environment

Create a new directory and Python file.

Create a file called form_filler.py (or any name that works for you) and add your configuration.

Replace your_api_key_here with your actual Gaffa API key from the Dashboardarrow-up-right.

Extract Form Fields Using parse_json

In the code below, you define a function that takes a form URL as input and makes a POST request to the Gaffa API.

The request uses two actions: first, a wait action ensures the form element is fully loaded on the page, then the parse_json action that uses AI to intelligently analyze the form structure and extract all input fields along with their properties (labels, names, types, placeholders, and required status). The AI understands the context of the form and returns structured JSON data that we can easily work with.

Collect User Input

Next, you need to define a function that takes the extracted form data and interacts with the user in the terminal. The function will display the form title and then loop through each field, prompting the user to fill in the value.

For each field in the form, a label and a required marker, if applicable, are shown. The function ensures that the required fields are not left empty and allows users to skip optional fields by pressing enter. All the user's input is collected into a dictionary where the keys are the field names and the values are what the user entered.

Fill and Submit the Form

You need a function that will take the form URL and the user's input values, then submit the form to Gaffa's browser automation. The function will build a list of actions.

First, it waits for the form to be ready, then creates a type action for each field to enter the user's value into the corresponding input element using CSS selectors. Lastly, it adds a click action to submit the form and a capture_screenshot action to take a full-screen image of the results.

The function makes a POST request with all these actions and returns the response, which includes the screenshot URL if successful.

User Interaction and Execution

Having defined the functions, we can now create a simple command-line interface that allows users to interact with the form.

Full Script

The full script is available to download from the Gaffa Python Examples GitHub repoarrow-up-right.

Running the Script

To run the script, simply execute it in your terminal:

Example output:

Last updated