mcp_tools / playwright_example.py
ZarakShah's picture
Upload folder using huggingface_hub
8a59f15 verified
Raw History Blame Contribute Delete
5.4 kB
"""
Example script demonstrating how to use Playwright MCP tools via MCP client.
This script shows how to interact with the Playwright MCP server tools
like browser_navigate, browser_take_screenshot, browser_click, etc.
"""
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def use_playwright_tools():
"""
Example function showing how to use Playwright MCP tools.
"""
# Configure the Playwright MCP server
server_params = StdioServerParameters(
command="npx",
args=["@playwright/mcp@latest"],
)
# Connect to the MCP server
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the session
await session.initialize()
# List available tools
tools = await session.list_tools()
print("Available Playwright tools:")
for tool in tools.tools:
print(f" • {tool.name}")
# Example 1: Navigate to a website
print("\n--- Example: Navigating to a website ---")
navigate_result = await session.call_tool(
"browser_navigate",
arguments={
"url": "https://example.com"
}
)
print(f"Navigation result: {navigate_result.content}")
# Example 2: Take a screenshot
print("\n--- Example: Taking a screenshot ---")
screenshot_result = await session.call_tool(
"browser_take_screenshot",
arguments={}
)
print(f"Screenshot taken: {screenshot_result.content}")
# Example 3: Get browser snapshot (HTML)
print("\n--- Example: Getting browser snapshot ---")
snapshot_result = await session.call_tool(
"browser_snapshot",
arguments={}
)
print(f"Snapshot: {snapshot_result.content[:200]}...") # First 200 chars
# Example 4: Click on an element
print("\n--- Example: Clicking an element ---")
# Note: You'll need to provide a selector
click_result = await session.call_tool(
"browser_click",
arguments={
"selector": "a", # Example: click first link
"button": "left",
"clickCount": 1
}
)
print(f"Click result: {click_result.content}")
# Example 5: Type text into an input field
print("\n--- Example: Typing text ---")
type_result = await session.call_tool(
"browser_type",
arguments={
"selector": "input",
"text": "Hello, World!",
"delay": 100 # milliseconds between keystrokes
}
)
print(f"Type result: {type_result.content}")
# Example 6: Wait for an element
print("\n--- Example: Waiting for element ---")
wait_result = await session.call_tool(
"browser_wait_for",
arguments={
"selector": "h1",
"state": "visible",
"timeout": 5000 # 5 seconds
}
)
print(f"Wait result: {wait_result.content}")
# Example 7: Close the browser
print("\n--- Example: Closing browser ---")
close_result = await session.call_tool(
"browser_close",
arguments={}
)
print(f"Close result: {close_result.content}")
async def simple_browser_automation():
"""
A simple browser automation example using Playwright MCP tools.
"""
server_params = StdioServerParameters(
command="npx",
args=["@playwright/mcp@latest"],
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# Navigate to a page
await session.call_tool("browser_navigate", arguments={"url": "https://example.com"})
# Wait for page to load
await session.call_tool(
"browser_wait_for",
arguments={"selector": "body", "state": "visible"}
)
# Take a screenshot
result = await session.call_tool("browser_take_screenshot", arguments={})
print(f"Screenshot saved: {result.content}")
# Get page content
snapshot = await session.call_tool("browser_snapshot", arguments={})
print(f"Page title found in snapshot: {'Example Domain' in str(snapshot.content)}")
# Close browser
await session.call_tool("browser_close", arguments={})
if __name__ == "__main__":
print("Playwright MCP Tools Example")
print("=" * 50)
# Run the simple example
asyncio.run(simple_browser_automation())
# Uncomment to run the detailed example:
# asyncio.run(use_playwright_tools())