File size: 5,397 Bytes
8a59f15 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
"""
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())
|