-
Notifications
You must be signed in to change notification settings - Fork 161
Add External browser (Tetra) examples #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
paveldudka
merged 5 commits into
main
from
pasha/tf-8540-update-examples-in-agentql-repo
Jun 24, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...th-external-or-existing-browser/README.md → examples/js/use-existing-browser/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| title: Use external browser for AgentQL queries | ||
| description: Browser is hosted remotely - you connect to it over CDP. | ||
| updated: 2025-06-24 | ||
| --- | ||
|
|
||
| # Example script: Use external browser with AgentQL | ||
|
|
||
| This example demonstrates how to use external browser with AgentQL. | ||
|
|
||
| ## Run the script | ||
|
|
||
| - [Install AgentQL SDK](https://docs.agentql.com/installation/sdk-installation) | ||
| - Save this JavaScript file locally as **main.js** | ||
| - Run the following command from the project's folder: | ||
|
|
||
| ```bash | ||
| node main.js | ||
| ``` | ||
|
|
||
| Script will take care of allocating AgentQL External Browser, connecting to it and running the AgentQL query. | ||
|
|
||
| ## Play with the query | ||
|
|
||
| Install the [AgentQL Debugger Chrome extension](https://docs.agentql.com/installation/chrome-extension-installation) to play with the AgentQL query. [Learn more about the AgentQL query language](https://docs.agentql.com/agentql-query/query-intro) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * This example demonstrates how to interact with an external browser with AgentQL. | ||
| */ | ||
| import { wrap } from 'agentql'; | ||
| import { UserAgentPreset, createBrowserSession } from 'agentql/tools'; | ||
| import { chromium } from 'playwright'; | ||
|
|
||
| const URL = 'https://scrapeme.live/shop'; | ||
|
|
||
| const SEARCH_QUERY = ` | ||
| { | ||
| search_products_box | ||
| } | ||
| `; | ||
|
|
||
| const STOCK_QUERY = ` | ||
| { | ||
| number_in_stock | ||
| } | ||
| `; | ||
|
|
||
| async function getRemoteBrowserUrl() { | ||
| // This function creates a new browser session with Windows user agent preset | ||
| const session = await createBrowserSession(UserAgentPreset.WINDOWS); | ||
| const cdpUrl = session.cdpUrl; | ||
| console.log(`Remote browser URL: ${cdpUrl}`); | ||
| console.log(`Page streaming URL: ${session.getPageStreamingUrl(0)}`); | ||
| return cdpUrl; | ||
| } | ||
|
|
||
| async function runInExternalBrowser() { | ||
| // This function demonstrates how to open and interact with a new page using remote browser | ||
| const browserUrl = await getRemoteBrowserUrl(); | ||
|
|
||
| // Connect to the browser via Chrome DevTools Protocol | ||
| const browser = await chromium.connectOverCDP(browserUrl); | ||
|
|
||
| // Create a new tab in the browser window and wrap it to get access to the AgentQL's querying API | ||
| const page = await browser.newPage(); | ||
| const wrappedPage = await wrap(page); | ||
|
|
||
| await wrappedPage.goto(URL); | ||
|
|
||
| // Use queryElements() method to locate the search product box from the page | ||
| const searchResponse = await wrappedPage.queryElements(SEARCH_QUERY); | ||
|
|
||
| // Use Playwright's API to fill the search box and press Enter | ||
| await searchResponse.search_products_box.type('Charmander'); | ||
| await wrappedPage.keyboard.press('Enter'); | ||
|
|
||
| // Use queryData() method to fetch the stock number from the page | ||
| const stockResponse = await wrappedPage.queryData(STOCK_QUERY); | ||
|
|
||
| console.log(stockResponse); | ||
| await browser.close(); | ||
| } | ||
|
|
||
| runInExternalBrowser().catch(console.error); |
10 changes: 5 additions & 5 deletions
10
...th_external_or_existing_browser/README.md → ...les/python/use_existing_browser/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| title: Use external browser with AgentQL | ||
| description: Browser is hosted remotely - you connect to it over CDP. | ||
| updated: 2025-06-24 | ||
| --- | ||
|
|
||
| # Example script: Use external browser with AgentQL | ||
|
|
||
| This example demonstrates how to use external browser with AgentQL. | ||
|
|
||
| ## Run the script | ||
|
|
||
| - [Install AgentQL SDK](https://docs.agentql.com/installation/sdk-installation) | ||
| - Save this Python file locally as **main.py** | ||
| - Run the following command from the project's folder: | ||
|
|
||
| ```bash | ||
| python3 main.py | ||
| ``` | ||
|
|
||
| Script will take care of allocating AgentQL External Browser, connecting to it and running the AgentQL query. | ||
|
|
||
| ## Play with the query | ||
|
|
||
| Install the [AgentQL Debugger Chrome extension](https://docs.agentql.com/installation/chrome-extension-installation) to play with the AgentQL query. [Learn more about the AgentQL query language](https://docs.agentql.com/agentql-query/query-intro) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """This example demonstrates how to interact with an external browser with AgentQL.""" | ||
|
|
||
| import asyncio | ||
|
|
||
| import agentql | ||
| from agentql.tools.async_api import BrowserSession, UserAgentPreset, create_browser_session | ||
| from playwright.async_api import async_playwright | ||
|
|
||
| URL = "https://scrapeme.live/shop" | ||
|
|
||
| SEARCH_QUERY = """ | ||
| { | ||
| search_products_box | ||
| } | ||
| """ | ||
|
|
||
| STOCK_QUERY = """ | ||
| { | ||
| number_in_stock | ||
| } | ||
| """ | ||
|
|
||
|
|
||
| async def get_remote_browser_url() -> str: | ||
| """This function creates a new browser session.""" | ||
| # Create a browser session with Windows user agent preset | ||
| session: BrowserSession = await create_browser_session(ua_preset=UserAgentPreset.WINDOWS) | ||
| cdp_url = session.cdp_url | ||
| print(f"Remote browser URL: {cdp_url}") | ||
| print(f"Page streaming URL: {session.get_page_streaming_url(0)}") | ||
| return cdp_url | ||
|
|
||
|
|
||
| async def run_in_external_browser(): | ||
| """This function demonstrates how to open and interact with a new page using remote browser.""" | ||
| browser_url = await get_remote_browser_url() | ||
| async with async_playwright() as p: | ||
| # Connect to the browser via Chrome DevTools Protocol | ||
| browser = await p.chromium.connect_over_cdp(browser_url) | ||
|
|
||
| # Create a new tab in the browser window and wrap it to get access to the AgentQL's querying API | ||
| page = await browser.new_page() | ||
| page = await agentql.wrap_async(page) | ||
|
|
||
| await page.goto(URL) | ||
|
|
||
| # Use query_elements() method to locate the search product box from the page | ||
| response = await page.query_elements(SEARCH_QUERY) | ||
|
|
||
| # Use Playwright's API to fill the search box and press Enter | ||
| await response.search_products_box.type("Charmander") | ||
| await page.keyboard.press("Enter") | ||
|
|
||
| # Use query_data() method to fetch the stock number from the page | ||
| response = await page.query_data(STOCK_QUERY) | ||
|
|
||
| print(response) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(run_in_external_browser()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.