Playwright MCP Server - FAQ
提示词优化类
1.用户描述的步骤有登录要求,AI调用playwright_navigate,而不是预期的playwright_login
取决于项目需求,在当前LLP项目中playwright_navigate目前没有看到使用需求,可以移除。此外,优化工具描述,使AI更好地理解工具适用于什么场景,是一个比较好的选择。
例如在以下两个工具的定义中,description是暴露给AI的提示词,应该描述使用场景。优化后playwright_login描述为推荐使用的方式,并且用于处理完整的登录流程,playwright_navigate描述为需要用户显式声明一个url输入
{name: "playwright_login",description: "RECOMMENDED: Complete login automation that handles the entire flow (navigation, sign-in click, credential filling, submit, and verification). This should be used instead of separate navigate/click/fill actions for login. Match step: When|And \"([^\"]*)\" has logged in Portal-UI",inputSchema: {type: "object",properties: {url: { type: "string", description: "URL to navigate to for login" },accountKey: {type: "string",description: "Key identifies the specific account or role (e.g., 'account 0', 'admin') from the accounts file to use for execution."},navigationTimeout: {type: "number",description: "Navigation timeout in milliseconds",default: 30000}},required: ["url", "accountKey"]}},{name: "playwright_navigate",description: "Open a URL, don't hallucinate an url,always use an exact url or relative path that user specified. Try to use click action to navigate to page if you have no url first, clicking is the most popular way. Use playwright_login as priority for login and use playwright_click as priority for navigation of pages",inputSchema: {type: "object",properties: {url: { type: "string", description: "URL or relative path the user figured out, do not get value by guess" },browserType: { type: "string", description: "Browser type to use (chromium, firefox, webkit). Defaults to chromium", enum: ["chromium", "firefox", "webkit"] },width: { type: "number", description: "Viewport width in pixels (default: 1280)" },height: { type: "number", description: "Viewport height in pixels (default: 720)" },timeout: { type: "number", description: "Navigation timeout in milliseconds" },waitUntil: { type: "string", description: "Navigation wait condition" },headless: { type: "boolean", description: "Run browser in headless mode (default: false)" }},required: ["url"],},},
2.某类操作在iframe中,但AI没有使用iframe相关tool,一直尝试常规tool
该项目中所有涉及examination的操作都在iframe中,因此将github.copilot.chat.instructions提示词优化如下:
{"text": "# Playwright Tool Usage:\n ## [IMPORTANT]Use playwright_get_visible_frame_structure first, then use playwright_get_iframe_clickable_elements/playwright_get_iframe_text_elements/playwright_get_iframe_fillable_elements accordingly for operating below iframe parts: 1.Contents under Examination tab\n ## [IMPORTANT]Use playwright_verify_iframe_clickable_element/playwright_verify_iframe_fillable_element/playwright_verify_iframe_text_element accordingly for validating below iframe parts: 1.Contents under Examination tab"
}
工具迭代优化
1.exam的页面验证,verify_text_element和playwright_get_iframe_text_elements都没有获取到目标元素
playwright是经过二次开发的开源MCP Server工具,尚未成熟和完善,这类问题是很常见的。需要具体分析和定位原因,可以启动@modelcontextprotocol/inspector工具,进入调试模式,确认是否工具返回的内容不符合预期。
若明确工具的功能不符合预期,需要修改工具的实现方法,通常是探测页面元素类工具,如
"playwright_get_clickable_elements",
"playwright_get_fillable_elements",
"playwright_get_text_elements",
"playwright_get_iframe_text_elements",
"playwright_get_iframe_clickable_elements",
"playwright_get_iframe_fillable_elements"
这几个大类,返回的页面元素不符合预期。 这种情况需要优化src/tools/browser/commonEvaluateCode.ts中的元素探测策略。需注意,在src/tools/browser/element.ts中iframe工具和非iframe工具,引用的是同一套代码,此处的改动需要通用和抽象。
配置文件
1.登录的账密信息在playwright_login方法调用中没有从配置文件中读取
在src\tools\utils\credentialsManager.ts中,getCredentials方法中实现了解析配置文件,账密信息必须存在在json的accounts关键字中,文件格式需要遵循以下示例:
{"accounts": {"user1": {"username": "user1@aos.com"},"user2": {"username": "user2@aos.com"},"user3": {"username": "user3@web.com"}}
}
工程启动
1.当用户提示词步骤中含hover操作,且playwright工具提供了hover相关操作tool,但未调用成功
当playwright版本迭代时,需要重新build一次代码,让AI Agent启动最新版本的playwright工具
npm run build
npm link
重新启动playwright工具时,通过序号递增重命名一下并保存,清除cached tools后再点击Start启动