PlayWright Browser Toolkit
Playwright is an open-source automation tool developed by
Microsoft
that allows you to programmatically control and automate web browsers. It is designed for end-to-end testing, scraping, and automating tasks across various web browsers such asChromium
,Firefox
, andWebKit
.
This toolkit is used to interact with the browser. While other tools (like the Requests
tools) are fine for static sites, PlayWright Browser
toolkits let your agent navigate the web and interact with dynamically rendered sites.
Some tools bundled within the PlayWright Browser
toolkit include:
NavigateTool
(navigate_browser) - navigate to a URLNavigateBackTool
(previous_page) - wait for an element to appearClickTool
(click_element) - click on an element (specified by selector)ExtractTextTool
(extract_text) - use beautiful soup to extract text from the current web pageExtractHyperlinksTool
(extract_hyperlinks) - use beautiful soup to extract hyperlinks from the current web pageGetElementsTool
(get_elements) - select elements by CSS selectorCurrentPageTool
(current_page) - get the current page URL
%pip install --upgrade --quiet playwright > /dev/null
%pip install --upgrade --quiet lxml
# If this is your first time using playwright, you'll have to install a browser executable.
# Running `playwright install` by default installs a chromium browser executable.
# playwright install
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m24.0[0m[39;49m -> [0m[32;49m24.2[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
Note: you may need to restart the kernel to use updated packages.
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m24.0[0m[39;49m -> [0m[32;49m24.2[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
Note: you may need to restart the kernel to use updated packages.
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
API Reference:PlayWrightBrowserToolkit
Async function to create context and launch browser:
from langchain_community.tools.playwright.utils import (
create_async_playwright_browser, # A synchronous browser is available, though it isn't compatible with jupyter.\n", },
)
API Reference:create_async_playwright_browser
# This import is required only for jupyter notebooks, since they have their own eventloop
import nest_asyncio
nest_asyncio.apply()