Appearance
Usage
Minimal async example
This is the recommended pattern. Create an async Playwright browser, pass it to the toolkit, get the tools, and hand them to any LangChain agent.
python
import asyncio
from playwright.async_api import async_playwright
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
# Build the toolkit and get all seven tools
toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=browser)
tools = toolkit.get_tools()
# Print available tool names
for tool in tools:
print(tool.name, "-", tool.description)
await browser.close()
asyncio.run(main())Expected output:
click_element - Click on an element with the given CSS selector
navigate_browser - Navigate a browser to the specified URL
previous_webpage - Navigate back to the previous web page
extract_text - Extract all the text on the current webpage
extract_hyperlinks - Extract all hyperlinks on the current webpage
get_elements - Retrieve elements in the current web page matching the given CSS selector
current_webpage - Returns the URL of the current pageSync example
python
from playwright.sync_api import sync_playwright
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
toolkit = PlayWrightBrowserToolkit.from_browser(sync_browser=browser)
tools = toolkit.get_tools()
# Navigate and extract text using the tools directly
nav_tool = next(t for t in tools if t.name == "navigate_browser")
text_tool = next(t for t in tools if t.name == "extract_text")
nav_tool.run("https://example.com")
text = text_tool.run("")
print(text[:500])
browser.close()Wiring into a LangChain agent
python
import asyncio
from playwright.async_api import async_playwright
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
from langchain.agents import AgentType, initialize_agent
from langchain_openai import ChatOpenAI
async def run_agent():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=browser)
tools = toolkit.get_tools()
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
agent_chain = initialize_agent(
tools,
llm,
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
)
result = await agent_chain.arun(
"Navigate to https://example.com and tell me the page title."
)
print(result)
await browser.close()
asyncio.run(run_agent())Key import paths
python
# Toolkit (recommended)
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
# Individual tools (if you only need a subset)
from langchain_community.tools.playwright import (
NavigateTool,
NavigateBackTool,
ClickTool,
ExtractTextTool,
ExtractHyperlinksTool,
GetElementsTool,
CurrentWebPageTool,
)Notes
- You must pass either
async_browserorsync_browsertoPlayWrightBrowserToolkit(or itsfrom_browserclassmethod). Passing neither raises aValueError. - The
NavigateToolrestricts URL schemes tohttpandhttpsby default to prevent access to local files. Overrideargs_schemato change this. - For agents using
asyncLLM calls (most modern chains), prefer the async browser path.