agentql icon indicating copy to clipboard operation
agentql copied to clipboard

resolve AttributeError typo in python async template (search_btn)

Open codeCraft-Ritik opened this issue 2 months ago • 0 comments

GITHUB ISSUE BODY:

🐛 Bug Description

In .templates/python/template_async.py, there is an attribute name mismatch between the AgentQL query and the element interaction code:

  1. Attribute Name Mismatch: The GraphQL query in lines 39–44 defines the search button as search_btn:

    elements_query = """
    {
        search_input
        search_btn
    }
    """
    
  2. Runtime AttributeError on Execution: At line 50, the template attempts to click search_button instead of search_btn:

    await response.search_button.click()
    

    Because AgentQL's response object dynamically creates attributes matching the query keys, accessing response.search_button throws an unhandled exception:

    AttributeError: 'AgentQLResponse' object has no attribute 'search_button'
    

(Note: Compare with .templates/python/template_sync.py (line 43) and .templates/js/template.js (line 44) which correctly call response.search_btn.click()).


🔍 Steps to Reproduce

  1. Copy .templates/python/template_async.py to create a new script.
  2. Replace WEBSITE_URL_1 with any valid URL containing a search input and button.
  3. Run the async script with python template_async.py.
  4. Observe the runtime failure when line 50 is reached:
    AttributeError: 'AgentQLResponse' object has no attribute 'search_button'. Did you mean: 'search_btn'?
    

🛠️ Proposed Solution / Code Diff

--- a/.templates/python/template_async.py
+++ b/.templates/python/template_async.py
@@ -47,7 +47,7 @@ async def fetch_data(context: BrowserContext, session_url):
     response = await page.query_elements(elements_query)
     # Update to use the actual query terms to interact with the elements
     await response.search_input.type("<Replace with needed search query>")
-    await response.search_button.click()
+    await response.search_btn.click()
 
     # Update the query to fetch the desired data from the page
     data_query = """

📄 Full Fixed Template File (.templates/python/template_async.py):

"""This script serves as a skeleton template for asynchronous AgentQL scripts."""

import asyncio
import logging

import agentql
from playwright.async_api import BrowserContext, async_playwright

# Set up logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)

# Set URLs to the desired websites
WEBSITE_URL_1 = "<Replace with the correct url>"
WEBSITE_URL_2 = "<Replace with the correct url>"
WEBSITE_URL_3 = "<Replace with the correct url>"


async def main():
    """Fetch data concurrently in the same browser session from multiple websites."""
    async with async_playwright() as p, await p.chromium.launch(
        headless=False
    ) as browser, await browser.new_context() as context:
        # Open multiple tabs in the same browser context to fetch data concurrently
        await asyncio.gather(
            fetch_data(context, WEBSITE_URL_1),
            fetch_data(context, WEBSITE_URL_2),
            fetch_data(context, WEBSITE_URL_3),
        )


async def fetch_data(context: BrowserContext, session_url):
    """Open the given URL in a new tab and fetch the data."""
    # Create a page in a new tab in the broswer context and wrap it to get access to the AgentQL's querying API
    page = await agentql.wrap_async(await context.new_page())
    await page.goto(session_url)

    # Update the query to locate the desired element on the page
    elements_query = """
    {
        search_input
        search_btn
    }
    """

    # Locate desired web elements using AgentQL's query_elements() method
    response = await page.query_elements(elements_query)
    # Update to use the actual query terms to interact with the elements
    await response.search_input.type("<Replace with needed search query>")
    await response.search_btn.click()

    # Update the query to fetch the desired data from the page
    data_query = """
    {
        products[] {
            name
            price(integer)
        }
    }
    """

    # Fetch the data from the page using AgentQL's query_data() method
    data = await page.query_data(data_query)
    # Update to use the actual keys corresponding to query terms
    log.info(f"Prices fetched from {session_url}:")
    for product in data["products"]:
        log.info(f"Product: {product['name']}, Price: {product['price']}")


if __name__ == "__main__":
    # Run the main function in an event loop
    asyncio.run(main())

GITHUB PULL REQUEST (PR) TITLE:

fix(templates): fix search_btn attribute typo in async python template (#issue_number)

GITHUB PULL REQUEST (PR) DESCRIPTION:

Summary of Changes

  • Fixed Attribute Typo: Changed response.search_button.click() to response.search_btn.click() in .templates/python/template_async.py to match the query identifier search_btn.
  • Awaited Async Context Page: Updated agentql.wrap_async(context.new_page()) to await the new page coroutine.
  • Consistency: Aligned the Python async template with .templates/python/template_sync.py and .templates/js/template.js.

codeCraft-Ritik avatar Sep 07 '26 04:09 codeCraft-Ritik