Submitting a form
Submitting web forms is a common scenario in automation since it allows you to efficiently transfer data and run multiple scenarios.
Overview
In this guide, you will how to use an Playwright methods in a AgentQL script to automate submitting a form.
Common scenarios
Here are some common scenarios for filling out forms with Playwright and AgentQL.
Fill out an input field
Use Playwright's fill()
to fill an text input field. It accepts the desired value as an argument.
fill()
is the recommended method for filling out forms, but some forms fire events on keypress. In that case, use press_sequentially()
. Don't use type()
—it's deprecated.
await response.first_name.fill("John")
await response.date_of_birth.press_sequentially("2010-10-10")
Select an option from a dropdown or selection box
Use Playwright's selection_option()
method to select an option from a dropdown or select box. It accepts an option (or set of options for multiselect). See Playwright's docs for more.
await response.school.select_option(label="Washington University in St.Louis")
Upload a file
Use Playwright's FileChooster
to upload a file to a form.
{
resume_attach_btn
}
file_path = "/path/to/your-file.pdf"
response = await page.query_elements(query)
async with page.expect_file_chooser() as fc_info:
await response.resume_attach_btn.click()
file_chooser = await fc_info.value
await file_chooser.set_files(file_path)
Conclusion
Automating form submission allows you to unlock powerful workflows with AgentQL.
Here is a complete script that include examples of filling out the form and selecting options using this test form website.
import asyncio
import agentql
from playwright.async_api import async_playwright
# URL of the e-commerce website
# You can replace it with any other e-commerce website but the queries should be updated accordingly
URL = "https://formsmarts.com/html-form-example"
async def main():
"""Main function."""
async with async_playwright() as playwright, await playwright.chromium.launch(
headless=False
) as browser:
# Create a new page in the browser and wrap it to get access to the AgentQL's querying API
page = await agentql.wrap_async(browser.new_page())
await page.goto(URL) # open the target URL
form_query = """
{
first_name
last_name
email
subject_of_inquiry
inquiry_text_box
submit_btn
}
"""
response = await page.query_elements(form_query)
await response.first_name.fill("John")
await response.last_name.fill("Doe")
await response.email.fill("johndoe@agentql.com")
await response.subject_of_inquiry.select_option(label="Sales Inquiry")
await response.inquiry_text_box.fill("I want to learn more about AgentQL")
# Submit the form
await response.submit_btn.click()
# confirm form
confirm_query = """
{
confirmation_btn
}
"""
response = await page.query_elements(confirm_query)
await response.confirmation_btn.click()
await page.wait_for_page_ready_state()
await page.wait_for_timeout(3000) # wait for 3 seconds
print("Form submitted successfully!")
if __name__ == "__main__":
asyncio.run(main())