Skip to content

Commit

Permalink
Fix: Change Playwright browser tool from sync to async (#17808)
Browse files Browse the repository at this point in the history
  • Loading branch information
jisonZ authored Feb 13, 2025
1 parent 50a4a9d commit b4a4716
Show file tree
Hide file tree
Showing 7 changed files with 580 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

This tool is a wrapper around the Playwright library. It allows you to navigate to a website, extract text and hyperlinks, and click on elements.

> **Warning**
> Only support async functions and playwright browser APIs.
## Installation

```
Expand All @@ -10,55 +13,65 @@ pip install llama-index-tools-playwright

## Setup

In order to use this tool, you need to have a sync Playwright browser instance. You can hook one up by running the following code:
In order to use this tool, you need to have a async Playwright browser instance. You can hook one up by running the following code:

```python
browser = PlaywrightToolSpec.create_sync_playwright_browser(headless=False)
playwright_tool = PlaywrightToolSpec.from_sync_browser(browser)
browser = PlaywrightToolSpec.create_async_playwright_browser(headless=False)
playwright_tool = PlaywrightToolSpec.from_async_browser(browser)
```

## Usage

### Navigate to a website

```python
playwright_tool.navigate_to("https://playwright.dev/python/docs/intro")
await playwright_tool.navigate_to("https://playwright.dev/python/docs/intro")
```

### Navigate back

```python
await playwright_tool.navigate_back()
```

### Get current page URL

```python
playwright_tool.get_current_page()
await playwright_tool.get_current_page()
```

### Extract all hyperlinks

```python
playwright_tool.extract_hyperlinks()
await playwright_tool.extract_hyperlinks()
```

### Extract all text

```python
playwright_tool.extract_text()
await playwright_tool.extract_text()
```

### Get element attributes

```python
element = playwright_tool.get_elements(
element = await playwright_tool.get_elements(
selector="ELEMENT_SELECTOR", attributes=["innerText"]
)
```

### Click on an element

```python
playwright_tool.click(selector="ELEMENT_SELECTOR")
await playwright_tool.click(selector="ELEMENT_SELECTOR")
```

### Fill in an input field

```python
playwright_tool.fill(selector="ELEMENT_SELECTOR", value="Hello")
await playwright_tool.fill(selector="ELEMENT_SELECTOR", value="Hello")
```

## Agentic Usage

This tool has a more extensive example usage documented in a Jupyter notebook [here](https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/tools/llama-index-tools-playwright/examples/playwright_browser_agent.ipynb)

This file was deleted.

Loading

0 comments on commit b4a4716

Please sign in to comment.