Skip to content

Commit d5e4074

Browse files
committed
simpler code
1 parent ff91c0a commit d5e4074

File tree

1 file changed

+16
-46
lines changed

1 file changed

+16
-46
lines changed

application_examples/submit-form/submit_form.py

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,6 @@
1111
# You can replace it with any other e-commerce website but the queries should be updated accordingly
1212
URL = "https://formsmarts.com/html-form-example"
1313

14-
form_text_input = {
15-
"first_name": "John",
16-
"last_name": "Doe",
17-
"email": "johndoe@agentql.com",
18-
"inquiry_text_box": "I want to learn more about AgentQL",
19-
}
20-
21-
form_select_input = {
22-
"subject_of_inquiry": "Sales Inquiry",
23-
}
24-
25-
26-
def construct_query(form_input):
27-
"""Construct the query string based on form_input keys."""
28-
fields = "\n".join(form_input.keys())
29-
return f"""
30-
{{
31-
{fields}
32-
submit_btn
33-
}}
34-
"""
35-
36-
37-
async def fill_form(response, form_text_input: dict):
38-
"""Fill the form with form_text_input."""
39-
for key, value in form_text_input.items():
40-
await getattr(response, key).fill(value)
41-
42-
43-
async def select_option(response, form_select_input: dict):
44-
"""Select the form_select_input."""
45-
for key, value in form_select_input.items():
46-
print(f"Selecting {key} with value: {value}")
47-
await getattr(response, key).select_option(label=value)
48-
4914

5015
async def main():
5116
"""Main function."""
@@ -56,18 +21,23 @@ async def main():
5621
page = await agentql.wrap_async(browser.new_page())
5722
await page.goto(URL) # open the target URL
5823

59-
form_input = {**form_text_input, **form_select_input}
60-
61-
query = construct_query(form_input)
62-
print(f"Query: {query}")
63-
64-
await page.wait_for_timeout(3000) # wait for 3 seconds
65-
response = await page.query_elements(query)
66-
67-
await fill_form(response, form_text_input)
68-
await select_option(response, form_select_input)
24+
form_query = """
25+
{
26+
first_name
27+
last_name
28+
email
29+
subject_of_inquiry
30+
inquiry_text_box
31+
submit_btn
32+
}
33+
"""
34+
response = await page.query_elements(form_query)
6935

70-
await response.wait_for_timeout(3000) # wait for 3 seconds
36+
await response.first_name.fill("John")
37+
await response.last_name.fill("Doe")
38+
await response.email.fill("johndoe@agentql.com")
39+
await response.subject_of_inquiry.select_option(label="Sales Inquiry")
40+
await response.inquiry_text_box.fill("I want to learn more about AgentQL")
7141

7242
# Submit the form
7343
await response.submit_btn.click()

0 commit comments

Comments
 (0)