-
Notifications
You must be signed in to change notification settings - Fork 68
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
Zechereh
merged 4 commits into
add_js_sdk_examples_folder
from
zach/tf-3916-application-xpath
Nov 4, 2024
Merged
Changes from all commits
Commits
Show all changes
4 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 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,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) |
This file contains 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,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(); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main()
needs to be awaitedThere was a problem hiding this comment.
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