仅知目标URL无IP时,如何解决ChromeDriver本地连接超时问题?
问题背景
我正在跟着Applitools旗下TAU平台的教程学习自动化测试,使用IntelliJ社区版作为IDE。为练习TestNG,编写了如下代码用于打开练习网站:
package com.testautomationu.chp3annotations.Configuration; //Class to exercise in testing using the BeforeMethod and AfterMethod notation import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; public class BM_AM_OrangeHRM { WebDriver driver; @BeforeMethod public void setUp() { System.setProperty("webdriver.chrome.whitelistedIps", ""); System.setProperty("webdriver.chrome.driver", "C:\\Users\\Carlo Cumino\\Downloads\\Drivers\\chromedriver-win64" + "\\chromedriver.exe"); driver= new ChromeDriver(); driver.get("https://opensource-demo.orangehrmlive.com/"); } @Test public void signIn(){ WebElement textUser= driver.findElement(By.id("txtUsername")); textUser.sendKeys("Admin"); WebElement textPassowrd= driver.findElement(By.id("txtPassword")); textPassowrd.sendKeys("Admin123"); WebElement buttonLogin = driver.findElement(By.id("btnLogin")); buttonLogin.click(); } @AfterMethod public void tearDown(){ driver.quit(); } }
尽管Chrome浏览器与ChromeDriver版本匹配(均为130.0.****),运行时控制台仍输出以下内容:
Starting ChromeDriver 131.0.6778.24 (970493e0d1f268b99c3e386d3bd2ee99719089d3-refs/branch-heads/6778@{#1222}) on port 19181 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully on port 19181. nov 04, 2024 4:57:36 PM org.openqa.selenium.net.UrlChecker$1 call INFO: Polling http://localhost:19181/healthz nov 04, 2024 4:57:36 PM org.openqa.selenium.net.UrlChecker$1 call INFO: Polling http://localhost:19181/healthz nov 04, 2024 4:57:37 PM org.openqa.selenium.net.UrlChecker$1 call INFO: Polling http://localhost:19181/healthz
最终出现如下运行时异常:
INFO: Polling http://localhost:19181/healthz java.lang.RuntimeException: Timed out waiting for http://localhost:19181/healthz to be available after 20058 ms
我的疑问:我知道Chrome有IP白名单机制,但练习仅提供目标网站URL(https://opensource-demo.orangehrmlive.com/)而非IP地址,该如何解决此超时问题?另外,我下载的ChromeDriver相关Selenium库似乎没有窗口管理命令,不确定是否与此问题相关。
解决方法
1. 修复ChromeDriver版本不匹配问题
从控制台输出能看到,实际运行的ChromeDriver是131.0.6778.24,但你说浏览器是130版本,这才是核心问题——版本不兼容导致连接超时。哪怕你下载了130的Driver,可能路径配置错误,或者系统里有旧版本Driver被优先调用。
最省心的解决方式是移除手动Driver配置,用Selenium Manager自动管理(Selenium 4.6+自带该功能):
@BeforeMethod public void setUp() { // 删除手动设置webdriver.chrome.driver的代码 // 删除webdriver.chrome.whitelistedIps的无效配置 driver = new ChromeDriver(); driver.get("https://opensource-demo.orangehrmlive.com/"); }
Selenium Manager会自动下载匹配浏览器版本的Driver,无需手动配置路径,彻底避免版本不兼容问题。
2. 正确配置IP白名单(若坚持手动管理Driver)
IP白名单是控制ChromeDriver允许哪些IP的客户端连接,和目标网站的IP无关,不用填目标网站地址。你之前设为空字符串是无效配置,改成以下任意一种:
- 允许本地连接:
System.setProperty("webdriver.chrome.whitelistedIps", "127.0.0.1,localhost"); - 测试环境下允许所有连接:
System.setProperty("webdriver.chrome.whitelistedIps", "*");
3. 窗口管理命令的问题说明
窗口管理命令(比如最大化窗口)属于Selenium核心API,和ChromeDriver无关,只要依赖正确就能用:
- 确保你的项目引入了完整的Selenium 4.x依赖
- 使用示例:
driver.manage().window().maximize(); // 最大化窗口 driver.manage().window().setSize(new Dimension(1920, 1080)); // 设置窗口大小
如果找不到相关方法,检查导入语句是否包含import org.openqa.selenium.WebDriver;,以及依赖是否正确同步到项目中。
内容的提问来源于stack exchange,提问作者Carlo Cumino

