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

如何使用Python Selenium的ActionChains模拟CTRL+SHIFT+2热键触发PhraseExpress表单

为什么ActionChains方案无法生效

Selenium 所有输入相关的 API(包括send_keys、ActionChains)的作用域仅限当前激活的浏览器进程内部,发送的按键事件只会传给浏览器渲染的DOM元素,不会进入系统全局输入队列。而PhraseExpress是监听操作系统全局的按键事件,自然无法捕获到Selenium发送的按键信号,因此无法触发表单弹出。
你用到的pyautogui.hotkey能生效,是因为pyautogui直接调用系统原生的输入接口,发送的事件会进入系统全局输入队列,能被所有监听全局热键的程序捕获。

跨Windows、Mac系统的解决方案

你可以继续使用pyautogui实现跨系统适配,只需要针对不同系统调整修饰键即可:Windows下全局热键的控制键为Ctrl,Mac下对应为Command,代码适配示例如下:

import platform
import pyautogui
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def open_form() -> None:
    """打开PhraseExpress对应表单的全局热键触发函数"""
    # 原有等待页面加载的逻辑可保留,不影响全局按键发送
    WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.TAG_NAME, "body")))
    
    if fullstring == "Note 2":
        system_platform = platform.system()
        if system_platform == "Windows":
            pyautogui.hotkey("ctrl", "shift", "2")
        elif system_platform == "Darwin":
            # Mac系统的platform返回值为Darwin,修饰键替换为command
            pyautogui.hotkey("command", "shift", "2")
        print("You opened PhraseExpress for Note 2")

注意事项

  • Mac系统下运行脚本需要先给执行脚本的终端/IDE开启「辅助功能」权限,路径为:系统设置→隐私与安全性→辅助功能,勾选对应程序即可。
  • 如果触发失败,先手动确认对应系统下PhraseExpress的全局热键设置是否和代码中的组合键一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 10:09:05