RemoteWebDriver首次请求HOST NOT RESOLVABLE问题排查求助
问题:Selenium RemoteWebDriver首次请求报HOST NOT RESOLVABLE,后续请求正常
我们在使用selenium-server-standalone 2.41搭配Firefox 28构建RemoteWebDriver场景时,出现仅首次请求抛出HOST NOT RESOLVABLE的WebDriver异常,后续所有请求均正常的问题。已调试两天未定位到根因,恳请指点排查方向,并请教该场景下的正确配置方式。
环境与配置详情
- 调用RemoteWebDriver的Machine A为Linux ARM64架构机器,使用
selenium-server-standalone-2.41.0.jar; - Machine B为Linux X86架构机器,Docker容器内同时运行Selenium Hub和Node(版本均为2.44.0),暴露4444端口,启动命令如下:
java -jar /u01/selenium/selenium-server-standalone-2.44.0.jar -role hub java -jar /u01/selenium/selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register
- 测试时从Machine A访问Machine B的4444端口。
Maven依赖
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server-standalone</artifactId> <version>2.41.0</version> </dependency>
测试代码(在Machine A执行)
import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class Main { public static void main(String[] args) throws Exception { DesiredCapabilities capability = DesiredCapabilities.firefox(); WebDriver driver = new RemoteWebDriver(new URL("http://<<MACHINEB>>:4444/wd/hub"), capability); driver.manage().window().maximize(); driver.get("http://localhost:4444"); Thread.sleep(10000); driver.close(); } }
注:因使用Selenium 2.41版本,未引入Geckodriver,符合官方文档要求。
排查方向
- DNS解析延迟问题:首次请求时Machine A的DNS缓存未命中,导致域名解析超时。可在测试前手动执行
ping <<MACHINEB>>预热DNS缓存,或检查ARM64系统的DNS配置(如/etc/resolv.conf)是否存在解析慢的情况。 - Selenium版本兼容性:Machine A用2.41、Machine B用2.44,跨版本可能导致首次握手时的协议不兼容。尝试将两端Selenium版本统一(均用2.41或2.44),验证问题是否消失。
- Docker网络初始化延迟:Machine B的Docker容器启动后,Hub/Node可能未完成网络绑定或注册流程,导致首次请求失败。可在启动Hub后等待30秒再启动Node,或通过访问
http://localhost:4444/grid/console确认Hub就绪后再启动Node。 - Firefox初始化延迟:首次启动Firefox 28时,浏览器自身的网络配置(如代理、DNS)可能未加载完成,引发首次请求异常。可在测试代码中先访问一个静态页面完成预热,再执行目标操作。
- TCP连接超时设置:ARM64机器网络性能可能弱于X86,Selenium默认的首次连接超时时间过短导致失败。可在创建RemoteWebDriver时延长超时时间:
import java.util.concurrent.TimeUnit; // 创建driver后添加超时配置 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
正确配置建议
- 统一Selenium版本:确保Machine A和Machine B使用相同版本的
selenium-server-standalone.jar,消除跨版本协议差异。 - 优化Docker启动流程:编写启动脚本,先启动Hub,轮询
http://localhost:4444/grid/console确认Hub就绪后,再启动Node。 - 指定Firefox路径:启动Node时明确指定Firefox 28的安装路径,避免自动查找带来的延迟:
java -jar /u01/selenium/selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register -Dwebdriver.firefox.bin=/usr/bin/firefox28
- 预热网络连接:在测试代码执行前,通过
curl http://<<MACHINEB>>:4444/wd/hub/status验证Machine A到Hub的连通性,确保网络已就绪。 - 配置本地DNS缓存:在Machine A上部署本地DNS缓存服务(如dnsmasq),减少首次域名解析的耗时。
内容的提问来源于stack exchange,提问作者forkdbloke
相关产品推荐
相关产品推荐

