Playwright Inspector是Playwright框架中自带的GUI工具,可以辅助开发者调试Playwright脚本。以下是Playwright Inspector的使用方法:
一、启动Playwright Inspector
Playwright Inspector可以通过以下几种方式启动:
-
设置环境变量PWDEBUG:
- 在运行Playwright脚本之前,设置环境变量
PWDEBUG=1
。这会使Playwright以调试模式运行,并自动打开Playwright Inspector。 - 对于不同的操作系统和命令行工具,设置环境变量的方式可能有所不同。例如,在bash中可以使用
PWDEBUG=1 pytest -s
,在PowerShell中可以使用$env:PWDEBUG=1 pytest -s
。
- 在运行Playwright脚本之前,设置环境变量
-
在脚本中添加page.pause():
- 在Playwright脚本中的适当位置添加
page.pause()
或await page.pause()
(对于异步代码)。当执行到这一行时,脚本会暂停,并自动打开Playwright Inspector。注意,这要求浏览器以有头模式(headed mode)启动。
- 在Playwright脚本中的适当位置添加
-
使用Playwright CLI:
- 通过Playwright的命令行接口(CLI),