首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Cline+Playwright-MCP配置浏览器

  • 25-04-20 20:01
  • 3013
  • 12657
juejin.cn

简介

本文档记录了在使用 Cline 和 Playwright-MCP 进行浏览器自动化时,如何配置和使用多种不同浏览器(如 Chrome, Edge, Firefox, WebKit)的过程。主要内容包括安装 Playwright 支持的各种浏览器、通过 Playwright 命令行工具 (npx playwright open) 启动指定浏览器,并最终解决了如何在 Playwright-MCP 环境下(其 browser_navigate 等工具本身不带浏览器选择参数)指定运行浏览器的关键问题——通过修改 cline_mcp_settings.json 配置文件实现。

探索如何配置

image-20250420053716642

mcp工具的browser_navigate只有一个url参数,无法指定浏览器。

image-20250420053850115

使用browser_navigate将会使用默认的浏览器打开网页。

image-20250420061128039

去playwright官方文档查看关于浏览器配置的说明。

powershell
代码解读
复制代码
PS A:\study\AI\MCP-Test> npx playwright install Downloading Chromium 134.0.6998.35 (playwright build v1161) from https://cdn.playwright.dev/dbazure/download/playwright/builds/chromium/1161/chromium-win64.zip Error: read ECONNRESET at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) { errno: -4077, code: 'ECONNRESET', syscall: 'read' } Downloading Chromium 134.0.6998.35 (playwright build v1161) from https://playwright.download.prss.microsoft.com/dbazure/download/playwright/builds/chromium/1161/chromium-win64.zip 141.8 MiB [====================] 100% 0.0s Chromium 134.0.6998.35 (playwright build v1161) downloaded to C:\Users\IU\AppData\Local\ms-playwright\chromium-1161 Downloading Chromium Headless Shell 134.0.6998.35 (playwright build v1161) from https://cdn.playwright.dev/dbazure/download/playwright/builds/chromium/1161/chromium-headless-shell-win64.zip 87.8 MiB [====================] 100% 0.0s Chromium Headless Shell 134.0.6998.35 (playwright build v1161) downloaded to C:\Users\IU\AppData\Local\ms-playwright\chromium_headless_shell-1161 Downloading Firefox 135.0 (playwright build v1475) from https://cdn.playwright.dev/dbazure/download/playwright/builds/firefox/1475/firefox-win64.zip 91.5 MiB [====================] 100% 0.0s Firefox 135.0 (playwright build v1475) downloaded to C:\Users\IU\AppData\Local\ms-playwright\firefox-1475 Downloading Webkit 18.4 (playwright build v2140) from https://cdn.playwright.dev/dbazure/download/playwright/builds/webkit/2140/webkit-win64.zip 52.8 MiB [====================] 100% 0.0s Webkit 18.4 (playwright build v2140) downloaded to C:\Users\IU\AppData\Local\ms-playwright\webkit-2140

使用npx playwright install命令安装默认的浏览器,可以看到我们安装了Chromium 134.0.6998.35、Chromium Headless Shell 134.0.6998.35、Firefox 135.0、Webkit 18.4。

image-20250420061541691

powershell
代码解读
复制代码
PS A:\study\AI\MCP-Test> npx playwright install --force msedge Downloading Microsoft Edge Installing Microsoft Edge ProductVersion FileVersion FileName -------------- ----------- -------- 135.0.3179.85 135.0.3179.85 C:\Program Files (x86)\Microsoft\Edge\...

使用上面的命令可以安装Edge浏览器。

powershell
代码解读
复制代码
PS A:\study\AI\MCP-Test> npx playwright help Usage: npx playwright [options] [command] Options: -V, --version output the version number -h, --help display help for command Commands: open [options] [url] open page in browser specified via -b, --browser codegen [options] [url] open page and generate code for user actions install [options] [browser...] ensure browsers necessary for this version of Playwright are installed uninstall [options] Removes browsers used by this installation of Playwright from the system (chromium, firefox, webkit, ffmpeg). This does not include branded channels. install-deps [options] [browser...] install dependencies necessary to run browsers (will ask for sudo permissions) cr [options] [url] open page in Chromium ff [options] [url] open page in Firefox wk [options] [url] open page in WebKit screenshot [options] capture a page screenshot pdf [options] save page as pdf run-server [options] show-trace [options] [trace...] show trace viewer test [options] [test-filter...] run tests with Playwright Test show-report [options] [report] show HTML report merge-reports [options] [dir] merge multiple blob reports (for sharded tests) into a single report clear-cache [options] clears build and test caches help [command] display help for command PS A:\study\AI\MCP-Test> npx playwright help open Usage: npx playwright open [options] [url] open page in browser specified via -b, --browser Options: -b, --browser browser to use, one of cr, chromium, ff, firefox, wk, webkit (default: "chromium") --block-service-workers block service workers --channel Chromium distribution channel, "chrome", "chrome-beta", "msedge-dev", etc --color-scheme emulate preferred color scheme, "light" or "dark" --device emulate device, for example "iPhone 11" --geolocation specify geolocation coordinates, for example "37.819722,-122.478611" --ignore-https-errors ignore https errors --load-storage load context storage state from the file, previously saved with --save-storage --lang specify language / locale, for example "en-GB" --proxy-server specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080" --proxy-bypass comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com" --save-har save HAR file with all network activity at the end --save-har-glob filter entries in the HAR by matching url against this glob pattern --save-storage save context storage state at the end, for later use with --load-storage --timezone time zone to emulate, for example "Europe/Rome" --timeout timeout for Playwright actions in milliseconds, no timeout by default --user-agent specify user agent string --viewport-size specify browser viewport size in pixels, for example "1280, 720" -h, --help display help for command Examples: $ open $ open -b webkit https://example.com

运行npx playwright help命令查看使用说明,发现open命令的--browser选项和指定浏览器有关,于是运行npx playwright help open命令查看关于启动时指定浏览器的说明,发现可以通过--channel指定使用的浏览器版本。

image-20250420054727526

powershell
代码解读
复制代码
PS A:\study\AI\MCP-Test> npx playwright open https://example.com PS A:\study\AI\MCP-Test> npx playwright open --browser chromium https://example.com

默认使用chrome浏览器打开网页,指定chromium内核时也是默认用chrome浏览器。

image-20250420054550771

powershell
代码解读
复制代码
PS A:\study\AI\MCP-Test> npx playwright open --browser chromium --channel msedge https://example.com

指定使用chromium内核的Edge浏览器打开网页。

image-20250420055126783

powershell
代码解读
复制代码
PS A:\study\AI\MCP-Test> npx playwright open --browser firefox https://example.com

指定使用火狐浏览器打开网页。

image-20250420055802740

powershell
代码解读
复制代码
PS A:\study\AI\MCP-Test> npx playwright open --browser webkit https://example.com

指定使用webkit浏览器打开网页。

image-20250420071342679

image-20250420071434563

由于mcp的browser_navigate函数无法指明使用的浏览器,调用该方法时仍然使用默认的chrome浏览器,没有解决问题。所以我们去playwright-mcp的github查找解决方法,得知可以在cline_mcp_settings.json配置文件中在args参数配置中指明使用的浏览器。

image-20250420071752002

json
代码解读
复制代码
{ "mcpServers": { "github.com/modelcontextprotocol/servers/tree/main/src/github": { "command": "cmd", "args": [ "/c", "npx", "-y", "@modelcontextprotocol/server-github" ], "env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "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" }, "disabled": false, "autoApprove": [] }, "playwright": { "command": "cmd", "args": [ "/c", "npx", "@playwright/mcp@latest", "--browser", "msedge" ], "autoApprove": [ "browser_navigate", "browser_type", "browser_click", "browser_snapshot", "browser_tab_select", "browser_tab_list", "browser_pdf_save", "browser_press_key", "browser_resize", "browser_tab_close" ] } } }

如图所示,使用--browser指明使用msedge。

演示视频

Cline+Playwright-mcp演示保存斋藤飞鸟图片

markdown
代码解读
复制代码
当前已配置了playwright-mcp和安装了所需的浏览器 1. 打开浏览器bing搜索页面,获取页面快照。 2. 搜索斋藤飞鸟,获取页面快照。 3. 找到斋藤飞鸟的图片,打开该图片链接,获取页面快照。 4. 将图片页面保存为pdf文件。

视频中将上述命令发给AI,并且设置Auto-approve开启了自动执行,从而实现AI调用playwright的工具对Edge浏览器进行自动化操作,搜索斋藤飞鸟老婆的图片并以pdf格式保存。

总结

通过本次探索,我们明确了在 Cline + Playwright-MCP 环境下使用不同浏览器的方法:

  1. 浏览器安装: 可以使用 npx playwright install 安装默认的 Chromium, Firefox, WebKit,或使用 npx playwright install (如 msedge) 安装特定浏览器。
  2. Playwright CLI 使用: npx playwright open 命令可以通过 -b 或 --browser 参数指定浏览器类型(chromium, firefox, webkit),并通过 --channel 参数进一步指定具体的分发版本(如 msedge, chrome)。
  3. Playwright-MCP 配置: 由于 Playwright-MCP 提供的 browser_* 工具没有直接指定浏览器的参数,关键在于修改 MCP 服务器的启动参数。在 cline_mcp_settings.json 文件中,为 playwright 服务器配置项的 args 数组添加相应的命令行参数,例如 "--browser", "firefox" 或 "--browser", "chrome-dev", "--browser", "msedge",即可让 MCP 启动并控制指定的浏览器。

这种配置方法使得我们可以灵活地让 AI 控制不同的浏览器执行自动化任务,满足不同的测试或操作需求。

注:本文转载自juejin.cn的Miku16的文章"https://juejin.cn/post/7494944356533796902"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top