Best Practices for AgentQL Queries
AgentQL queries allow users to retrieve the exact web page elements for interaction or data retrieval. Designed with flexibility in mind, AgentQL queries are schema-less, meaning query terms are free-form and not strongly typed. However, there are some syntax requirements as well as best practices for creating an AgentQL query.
AgentQL query syntax
The list below contains all syntax requirements for AgentQL Query:
- Enclose query with curly braces.
- Put new terms on new lines (i.e. one element per line).
- Do not separate terms with punctuation.
- A container should enclose its children terms with curly braces
{ ... }
. - Create a list term by adding closed brackets '[]' after the term.
- Provide extra descriptions in parentheses '()' after the term.
Query examples
This is an example of a Single Term Query that tries to retrieve one element (search box) on the web page:
Query with extra contexts
This is an example of providing extra contexts to query. A short description about the query term could be provided in parentheses to help AgentQL better locate the desired web elements:
Nested query
This is an example of a nested query that tries to get a "sign in" button from a page's header and an "about" button from its footer. In this case, <header />
and <footer />
elements serve as container elements that capture the hierarchical relationship of the desired elements.
Query that returns a list
This is an example of a list term query. This query tries to capture all the links on a web page. The AgentQL server returns an array of links for this query:
Nesting and lists combined
This is another example of a list term query. However, the query is specifying the exact information wanted in every list item. In this case, AgentQL server returns an array. Each array item contains the price, the rating, and reviews of one product.
Recommended practices for creating queries
AgentQL Query is designed to be flexible, but there are some recommended practices that may improve the response quality from AgentQL server:
- Use lowercase letters for all the terms in query.
- Use underscores (
_
) to separate words within a term (ieuser_image
). - Append
btn
to the term to indicate the element is a clickable (iesearch_btn
). - Append
box
to the name to indicate the element is inputtable (iesearch_box
). - Indent children terms in accordance with their parent's indentation.
- Only use
query_elements
when you wish to interact with web elements. Usequery_data
to retrieve data.
How to find the exact element on the page
When there are multiple elements with the same or similar names on the web page, the AgentQL server may need further hints from your AgentQL query to find the exact element you are looking for. There are several things you can do with your query to help improve AgentQL's accuracy.
Provide detailed descriptions
Providing descriptions in parentheses is a powerful tool to get better query results. For instance, providing the description of "google sign-in button" to the sign-in
term may help with targeting a specific button on a page.
Include hierarchy hints
Hierarchy hints reduce ambiguity. For example, if there are very similar buttons (f.i. “Sign In”) present on the web page, but one of them is positioned in the header and another one is in sign in form, you could try to specify such semantic information.
Consider the following example web page and queries:
Different containers (header
and form
) will convey different hierarchical information to AgentQL server and locate different sign-in buttons on this page.
Use surrounding terms
Another way to reduce ambiguity is to specify surrounding terms, so its more clear where the element is located. For example, if you are trying to locate “Sign in” button, which is placed between other 2 buttons, it may help to specify those other 2 buttons as well (even if you are not planning to interact with them) to clarify element location.
Examples
Here shows some examples of working with real web pages.
Retrieving Phone Model Button
In this example, the Agent query retrieves two buttons for different models by specifying their containing element.
Alternatively, you could query the model selector itself. The button information will be preserved in the response, so you could retrieve the buttons through parsing.
Retriving Amazon Product Information
This AgentQL query gets the name and price of each product on the Amazon product listing page.
If you want all the relevant information of each product, you could generalize the query above.