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

如何在AutoHotkey脚本中正确调用Chrome.ahk?含报错排查

Chrome.ahk #Include报错排查与修复方法

报错原因

  • 路径查找失败:你的Chrome.ahk存放在Documents/AutoHotKey/Lib/Chrome/子文件夹中,但AutoHotkey的#Include指令默认仅在当前脚本目录和标准Lib根目录(Documents/AutoHotKey/Lib/)下检索文件,不会自动遍历子文件夹,因此直接写#Include Chrome.ahk无法定位到目标文件。
  • 类实例化语法错误:代码中new Chrome.ahk.Chrome()的写法不符合AHK类调用规则,正确的实例化方式应为new Chrome()。
  • 调试端口缺失:Chrome.ahk依赖Chrome的远程调试端口建立连接,你原脚本中使用--guest启动Chrome,会导致无法开启调试模式,库无法正常工作。

正确调用方法与修正脚本

方法1:使用绝对路径Include

通过AHK内置变量A_MyDocuments直接指定Chrome.ahk的完整路径,避免路径查找问题:

#SingleInstance Force
#Include %A_MyDocuments%\AutoHotKey\Lib\Chrome\Chrome.ahk

#z::
    chromePath := "C:\Program Files\Google\Chrome\Application\chrome.exe"
    url := "https://Fprojects"
    
    ; 启动带远程调试端口的Chrome实例
    Run, %chromePath% --remote-debugging-port=9222 %url%
    WinWaitActive, ahk_class Chrome_WidgetWin_1,, 10
    if ErrorLevel{
        MsgBox, Chrome Not Open
        ExitApp
    }
    
    ; 实例化Chrome类并连接调试端口
    Page := new Chrome()
    Page.Connect("127.0.0.1", 9222)
    Page.WaitForLoad()
return

方法2:利用标准Lib子目录规则Include

使用<>符号让AHK从标准Lib目录开始查找子文件夹中的文件:

#SingleInstance Force
#Include <Chrome\Chrome.ahk>

#z::
    chromePath := "C:\Program Files\Google\Chrome\Application\chrome.exe"
    url := "https://Fprojects"
    
    Run, %chromePath% --remote-debugging-port=9222 %url%
    WinWaitActive, ahk_class Chrome_WidgetWin_1,, 10
    if ErrorLevel{
        MsgBox, Chrome Not Open
        ExitApp
    }
    
    Page := new Chrome()
    Page.Connect("127.0.0.1", 9222)
    Page.WaitForLoad()
return

关键注意事项

  • 必须确保Chrome启动时带有--remote-debugging-port=xxxx参数(端口号可自定义,只要不冲突即可),这是Chrome.ahk库与Chrome建立通信的必要条件。
  • 若使用方法2,需保证你的AutoHotkey已经正确识别Documents/AutoHotKey/Lib/为标准库目录(默认情况下AHK会自动识别此路径)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 11:12:41