You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium JS:切换DevTools面板标签页问题求助

问题解决指南

一、先修复Cannot read properties of undefined (reading 'COMMAND')错误

这个报错是因为你未正确导入Selenium的Key类,导致Key对象为undefined。根据你使用的JS模块规范,添加对应的导入语句:

  • CommonJS规范:
const { Key, Actions } = require('selenium-webdriver');
  • ES模块规范:
import { Key, Actions } from 'selenium-webdriver';

二、切换DevTools扩展面板的可靠方案

直接用快捷键操作不可靠——Selenium默认聚焦网页内容窗口,无法保证DevTools面板获得焦点。更稳定的方式是使用Chrome DevTools Protocol(CDP)直接控制DevTools:

1. 获取扩展面板的ID/名称

打开Chrome并启动你的扩展与DevTools,按Ctrl+Shift+I(Windows/Linux)或Cmd+Opt+I(Mac)打开DevTools自身的调试窗口,执行以下CDP命令获取所有面板信息:

const panels = await driver.executeCdpCommand('DevTools.getPanels', {});
console.log(panels); // 从输出中找到你的扩展面板对应的名称或ID

2. 直接切换到目标面板

使用DevTools.selectPanel命令完成切换:

await driver.executeCdpCommand('DevTools.selectPanel', {
  panelName: '你的扩展面板ID或名称'
});

备选:修复快捷键方案(仍需保证焦点)

如果坚持用快捷键,需先通过CDP激活DevTools确保焦点:

// 激活DevTools并指定停靠位置
await driver.executeCdpCommand('DevTools.show', { dockSide: 'right' });
// 执行快捷键(已修复Key导入问题后)
let actions = new Actions(driver);
await actions
  .keyDown(Key.COMMAND)
  .sendKeys('[')
  .keyUp(Key.COMMAND)
  .perform();

内容的提问来源于stack exchange,提问作者Guillaume

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 11:36:07