Automating Form Filling using Playwright Python

Aman Kumar
3 min readJun 6, 2024

--

Created using Dall-E

As web developers, we often need to test and interact with web forms. Whether it’s for testing purposes or automating repetitive tasks, being able to fill out forms programmatically can save a significant amount of time and effort. In this blog post, we’ll explore how to automate form-filling using Playwright Python, a powerful library for browser automation.

Playwright is a Python (Node.js, JAVA, .NET) library that provides a high-level API to automate Chromium, Firefox, and WebKit browsers. It allows you to write reliable end-to-end tests, crawl websites, and easily automate browser interactions.

We will setup playwright and learn how to use it to automate the process. Let's get started!

Setting up playwright

First use pip Command to install playwright

pip install playwright

Now, let’s look at an example that demonstrates how to fill out a Google Form using Playwright Python:

  1. We import the sync_playwright module from the playwright.sync_api package.
from playwright.sync_api import sync_playwright

2. We define a helper function fill_form that takes a page object, a label string, and a value string. This function uses the page.get_by_label Method to find the input field with the given label. We fill it with the provided value using the field.fill(value) Method.

def fill_form(page, label, value):
field = page.get_by_label(label)
field.fill(value)

3. We set the url Variable to the URL of the Google Form we want to fill out. The Google form contains name, email and message fields.

url = "https://forms.gle/fCzgvXeyLfVWP3bU6"
Form

4. We create a context manager using sync_playwright(), which launches a new browser instance.

with sync_playwright() as p:
# Launch the browser
browser = p.chromium.launch(headless=False, slow_mo=200)

5. Inside the context manager, we launch a new Chromium browser instance with browser = p.chromium.launch(headless=False, slow_mo=200). The headless=False argument makes the browser visible, and slow_mo=200 slows down the browser's actions by 200 milliseconds for better visibility.

6. We create a new page using browser.new_page() and navigate to the Google Form URL with page.goto(url).

    # Open a new page
with browser.new_page() as page:
# Navigate to the URL
page.goto(url)

7. We call the fill_form Function with the appropriate labels and values to fill out the form fields.

        # Fill out the form
fill_form(page, "Name", "John Doe")
fill_form(page, "Email", "johndoe@gmail.com")
fill_form(page, "Message", "Hello, this is a test message")

8. We locate the submit button using page.get_by_role("button", name="Submit") and store it in the btn variable.

        btn = page.get_by_role("button", name="Submit")

9. Click the button using btn.click().

        btn.click()

10. Finally, we close the browser with browser.close().

        # Close the browser
browser.close()

Finally, copy and paste the full code in main.py

Full Code

Finally, run

python main.py

If will open the chromium window, navigate through the inputs, fill them out and submit the form.

This example demonstrates how to automate form-filling using Playwright Python. You can modify the code to suit your specific requirements, such as handling different form types and dynamic content or integrating it with your testing framework.

Playwright Python provides a powerful and flexible API for automating browser interactions, making it an excellent choice for tasks like form-filling, web scraping, and end-to-end testing. With its rich features and cross-browser support, Playwright Python is a valuable tool in any web developer’s toolbox.

If this article was helpful, give it some claps. I’m deeply involved with AI and LLMs. Follow me on Medium for more insights.
Feel free to say hi or connect via Twitter and LinkedIn.

--

--