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

Selenium自动化Chrome认证弹窗失败:AutoIT脚本未执行求助

解决Selenium+AutoIT处理Basic Auth弹窗无响应的问题

我来帮你搞定这个Basic Auth弹窗的问题,你的AutoIT脚本没执行,大概率是弹窗标题不匹配或者执行时机不对导致的,咱们分两种方案来解决:

方案一:最简便的URL嵌入凭证法(推荐)

Basic Auth本身支持直接在URL里嵌入用户名和密码,这是最可靠的方法,完全不需要依赖AutoIT这类外部工具:

public class Authen { 
    public static void main(String[] args) { 
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Sumedha\\OneDrive\\Documents\\Selenium\\Drivers\\chromedriver_win32\\chromedriver.exe"); 
        WebDriver driver = new ChromeDriver(); 
        // 格式:http://用户名:密码@目标网址
        driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth"); 
    } 
}

如果Chrome新版本提示安全警告,你可以添加启动参数来允许这种访问:

public class Authen { 
    public static void main(String[] args) { 
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Sumedha\\OneDrive\\Documents\\Selenium\\Drivers\\chromedriver_win32\\chromedriver.exe"); 
        ChromeOptions options = new ChromeOptions();
        // 允许不安全内容访问
        options.addArguments("--allow-running-insecure-content");
        WebDriver driver = new ChromeDriver(options); 
        driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth"); 
    } 
}

方案二:修复AutoIT脚本问题

如果你坚持要用AutoIT,那咱们来排查并修复问题:

1. 确认弹窗的正确标题

Chrome的Basic Auth弹窗标题在不同语言/版本里可能不是Sign in,比如英文环境下通常是Authentication Required,中文环境可能是登录。你可以用AutoIT自带的Window Info工具(安装AutoIT后在开始菜单里能找到),点击弹窗查看准确的Title字段。

2. 修改AutoIT脚本

根据实际标题调整脚本,同时增加超时等待确保能捕获到弹窗:

; 等待弹窗出现,超时10秒(可调整)
WinWaitActive("Authentication Required", "", 10)
; 输入用户名
Send("admin")
; 切换到密码输入框
Send("{TAB}")
; 输入密码
Send("admin")
; 提交登录
Send("{ENTER}")

3. 调整Java代码的执行时机

在调用AutoIT之前,给弹窗足够的加载时间,避免脚本执行时弹窗还没出现:

public class Authen { 
    public static void main(String[] args) throws IOException, InterruptedException { 
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Sumedha\\OneDrive\\Documents\\Selenium\\Drivers\\chromedriver_win32\\chromedriver.exe"); 
        WebDriver driver = new ChromeDriver(); 
        driver.get("http://the-internet.herokuapp.com/basic_auth"); 
        // 等待2秒确保弹窗加载完成(时间可根据实际情况调整)
        Thread.sleep(2000);
        Runtime.getRuntime().exec("C:\\Users\\Sumedha\\OneDrive\\Documents\\Selenium\\AutoIT\\handleauthentication.exe"); 
    } 
}

额外注意事项

  • 确保你的AutoIT脚本已经正确编译成exe文件,或者直接调用au3脚本(需要系统安装AutoIT,用Runtime.getRuntime().exec("autoit3.exe C:\\路径\\到\\你的脚本.au3");)
  • 如果是Windows系统,可能需要以管理员权限运行你的Java程序,避免AutoIT无法操作系统弹窗。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:09:10