使用 Selenium 在 Chrome 中中断异常

Break on exception in Chrome using Selenium

为了调查一些 Selenium 测试失败,我想在 运行 测试时自动启用 pause on exception feature in the Chrome Devtools

--auto-open-devtools-for-tabs 命令行选项可以自动打开我已经在使用的 DevTools 窗格,但显然没有 CLI option/parameter 我正在寻找的自动暂停功能。

不过我遇到的是 Debugger.setPauseOnExceptions Chrome Devtools Protocol command which I tried activating using execute_cdp_cmd(我正在为 Python 使用 Selenium):

driver.execute_cdp_cmd("Debugger.setPauseOnExceptions", {"state": "all"})

不幸的是,即使选项卡处于打开状态(包括 DevTools 窗格),我也得到了

unhandled inspector error: {"code":-32000,"message":"Debugger agent is not enabled"}

我做错了什么或者有其他方法(最好是可靠且便携的方法,请不要宏)我可以使用吗?

您可能需要在命令之前启用调试器:

driver.execute_cdp_cmd("Debugger.enable", {})
driver.execute_cdp_cmd("Debugger.setPauseOnExceptions", {"state": "all"})