Fetch a collection of elements with query_elements
You can use the query_elements method to locate more than one element from a web page. You can use these elements to automate workflows and interact with web sites by simulating clicking on buttons, filling out form fields, and scrolling.
Unlike query_data(scraping), query_elements doesn't return data but one or more interactive Playwright locators.
Overview
This guide shows you how to return one or more elements with query_elements and how to use it in your scripts to interact with web elements.
Define a query
The first step is to define a query for all the desired elements you want AgentQL to return.
In this example, the goal is to interact with the add_to_cart_button.
{
add_to_cart_button
}Fetch an element with query_elements
Next, pass the query to the query_elements method to fetch the desired elements.
response = page.query_elements(QUERY)Access an element returned by query_elements
query_elements returns a Python object you can use to interact with the elements on the page. You can access the elements as if they were the fields of this response object.
response.add_to_cart_button.click()Fetch multiple elements with query_elements
This query returns all the products on the page and their "add to cart" buttons.
{
products[] {
add_to_cart_button
}
}Extract a single element from many
# Click first product's button on the page
response.products[0].add_to_cart_button.click()
# Click last product's button on the page
response.products[-1].add_to_cart_button.click()Conclusion
query_elements is the proper method to use when you want to interact with multiple elements on a page. If you only need to a single element, use the get_by_prompt method instead.