11
11
# You can replace it with any other e-commerce website but the queries should be updated accordingly
12
12
URL = "https://formsmarts.com/html-form-example"
13
13
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
-
49
14
50
15
async def main ():
51
16
"""Main function."""
@@ -56,18 +21,23 @@ async def main():
56
21
page = await agentql .wrap_async (browser .new_page ())
57
22
await page .goto (URL ) # open the target URL
58
23
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 )
69
35
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" )
71
41
72
42
# Submit the form
73
43
await response .submit_btn .click ()
0 commit comments