Skip to content
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

Add xpath application example #92

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions javascript-sdk/application-examples/xpath/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Example script: get XPath of elements fetched with AgentQL

This example demonstrates how to get XPath of an element that was fetched with AgentQL.

## Run the script

- [Install AgentQL SDK](https://docs.agentql.com/javascript-sdk/installation)
- [Install Playwright Dompath](https://www.npmjs.com/package/playwright-dompath) with the following command:

```bash
npm install playwright-dompath
```

- Save this Javascript file locally as **xpath.js**
- Run the following command from the project's folder:

```bash
node xpath.js
```

## 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)
37 changes: 37 additions & 0 deletions javascript-sdk/application-examples/xpath/xpath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* This example demonstrates how to get XPath of an element that was fetched with AgentQL */

const { wrap } = require('agentql');
const { chromium } = require('playwright');

// Import the xPath function from the playwright-dompath package.
const { xPath } = require('playwright-dompath');

// Define the URL of the page to scrape.
const URL = 'https://scrapeme.live/shop/';

// Define the query to locate the search box.
const QUERY = `
{
search_products_box
}
`;

async function main() {
// Launch a headless browser using Playwright.
const browser = await chromium.launch({ headless: false });

// Create a new page in the browser and wrap it to get access to the AgentQL's querying API
const page = await wrap(await browser.newPage());
await page.goto(URL);

// Use queryElements() method to locate the search box from the page.
const response = await page.queryElements(QUERY);

// Get the XPath of the search box element.
console.log('XPath:', await xPath(response.search_products_box));

// Close the browser.
await browser.close();
}

main();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main() needs to be awaited

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

False alarm, this should not be awaited

19 changes: 18 additions & 1 deletion javascript-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion javascript-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"agentql": "^0.0.1",
"playwright": "^1.48.2"
"playwright": "^1.48.2",
"playwright-dompath": "^0.0.7"
}
}
Loading