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

Selenide中执行Cmd/Ctrl+Shift+U快捷键无响应问题求助

解决Selenide中Chromium快捷键无响应问题(需窗口最小化/最大化才生效)

问题现象

在Selenide中尝试通过Cmd(Mac)/Ctrl(Windows)+Shift+U快捷键打开Wave WCAG扩展时,Chromium浏览器完全无响应;手动操作该快捷键也需要先对窗口执行最小化+最大化后才能生效,文本框复制粘贴等其他快捷键也存在类似问题。

原因分析

这类问题通常是因为Chromium在Selenium/Selenide控制下,浏览器窗口未获得焦点,导致系统级的快捷键事件无法被正确捕获。即使页面加载完成,浏览器进程可能处于后台焦点状态,需要强制触发窗口焦点切换。

解决方案

方案1:强制窗口获取焦点

模拟手动最小化+最大化的操作,强制浏览器窗口获得焦点,再执行快捷键:

// 在执行快捷键前添加以下代码
driver.manage().window().minimize();
driver.manage().window().maximize();
// 可选:等待窗口状态稳定
Thread.sleep(500);

方案2:直接调用扩展API(推荐,最可靠)

快捷键本质是触发扩展的内置命令,Wave扩展支持通过Chrome的chrome.runtime.sendMessage直接调用,无需依赖窗口焦点和快捷键:

  1. 确认Wave扩展ID(官方ID为jbbplnpkjmmeebjpijfedlgcdilocofh)
  2. 使用Selenide的JS执行能力触发扩展:
// 直接触发Wave扩展的显示/隐藏命令
Selenide.executeJavaScript("chrome.runtime.sendMessage('jbbplnpkjmmeebjpijfedlgcdilocofh', {action: 'toggle'});");

方案3:将快捷键事件绑定到页面元素

确保快捷键事件发送到已获得焦点的页面元素(如body),避免事件丢失:

// 先点击body确保元素获得焦点
$(By.tagName("body")).click();
// 再执行快捷键操作
new Actions(driver)
    .keyDown(cmdCtrl)
    .keyDown(Keys.SHIFT)
    .sendKeys("u")
    .keyUp(Keys.SHIFT)
    .keyUp(cmdCtrl)
    .perform();

修改后的完整代码示例

package com.example;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.Selenide;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;

import java.io.File;

import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.$;

public class Main {

    public static void main(String[] args) throws Exception {
        Configuration.baseUrl = "https://google.com";

        // 加载Wave扩展
        Configuration.browserCapabilities = new ChromeOptions()
                .addExtensions(new File("src/main/resources/wave.crx"));

        open("/");

        WebDriver driver = WebDriverRunner.getWebDriver();
        Keys cmdCtrl = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;

        // 方案1:强制窗口获取焦点
        driver.manage().window().minimize();
        driver.manage().window().maximize();
        Thread.sleep(500);

        // 方案3:绑定到body元素执行快捷键
        $(By.tagName("body")).click();
        new Actions(driver)
                .keyDown(cmdCtrl)
                .keyDown(Keys.SHIFT)
                .sendKeys("u")
                .keyUp(Keys.SHIFT)
                .keyUp(cmdCtrl)
                .perform();

        // 或者使用方案2的API调用(注释掉上面的快捷键代码,启用下面一行)
        // Selenide.executeJavaScript("chrome.runtime.sendMessage('jbbplnpkjmmeebjpijfedlgcdilocofh', {action: 'toggle'});");

        Thread.sleep(20_000L);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 15:16:05