AQLResponseProxy
The AQLResponseProxy class is returned by AgentQL Page's query_elements() method. Not the actual data, AQLResponseProxy is a metadata that allows for intuitive access to web elements using dot notation. But this class can be converted into raw data as structured dictionary through its to_data()
method.
To access desired web elements, users can directly use the names defined in queries as attributes of the response object. It will return desired elements as Playwright Locator objects, and users can interact with these elements, such as click or type, through Playwright Locator API.
The following example queries for web elements through query_elements()
method, interacts with these elements through AQLResponseProxy
objects, and converts AQLResponseProxy
objects into raw data.
Methods
to_data
Converts the response data into a structured dictionary based on the query tree.
Usage
aql_response = page.query_elements(QUERY)
aql_response.to_data()
Returns
A structured Python dictionary in the following format.
{
"query_field": "text content of the corresponding web element"
}
__getattr__
If this method is called on an innermost node of the query, it will return the desired web element as a Playwright Locator object. Please check the Playwright API reference to see available methods in Locator
class.
If it is called on a container node of the query, it will return another AQLResponseProxy
object, which can be further interacted to get Playwright Locator
object.
Usage
QUERY = """
{
search_btn
search_results[]
}
"""
aql_response = page.query_elements(QUERY)
# This invokes Playwright Locator object's click() method
aql_response.search_btn.click()
# This iterate through search result with AQLResponseProxy
for search_result in aql_response.search_results:
print(search_result)
Arguments
name
str
The name of the attribute to retrieve.
Returns
__getitem__
Allows indexing into the response data if it's a list.
Usage
QUERY = """
{
search_results[]
}
"""
aql_response = page.query_elements(QUERY)
# Get the second result in the list
second_result = aql_response.search_results[1]
Arguments
index
int
The index of the item in the list to retrieve.
Returns
The corresponding AQLResponseProxy
list item.
__len__
Returns the number of items in the response data if it is a list.
Usage
QUERY = """
{
search_results[]
}
"""
aql_response = page.query_elements(QUERY)
# Get the number of search results
result_count = len(aql_response.search_results)
Returns
Number of items in the list.
__str__
Returns a string representation of the response data in JSON format.
Usage
QUERY = """
{
search_results[]
}
"""
aql_response = page.query_elements(QUERY)
# Convert results to a JSON string for display
result_in_json_string = print(aql_response.search_results)