求助:Selenium ChromeDriver在类Unix系统中出现空会话问题
ChromeDriver在类Unix系统(Mac/CentOS7)运行异常的解决办法
问题背景
开发的Selenium网页读取应用在Windows系统运行正常,但在Mac、CentOS7等类Unix系统中无法工作,日志出现Forwarding newSession on session null to remote提示,相关警告及日志如下:
Starting ChromeDriver 102.0.5005.61 (0e59bcc00cc4985ce39ad31c150065f159d95ad3-refs/branch-heads/5005@{#819}) on port 53751 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully. [1658989503.540][WARNING]: This version of ChromeDriver has not been tested with Chrome version 103. 28-Jul-2022 10:55:03.613 INFO [Forwarding newSession on session null to remote] org.openqa.selenium.remote.ProtocolHandshake.createSession Detected dialect: W3C 28-Jul-2022 10:55:03.673 WARNING [main] org.openqa.selenium.devtools.CdpVersionFinder.findNearestMatch Unable to find an exact match for CDP version 103, so returning the closest version found: a no-op implementation 28-Jul-2022 10:55:03.673 INFO [main] org.openqa.selenium.devtools.CdpVersionFinder.findNearestMatch Unable to find CDP implementation matching 103. 28-Jul-2022 10:55:03.674 WARNING [main] org.openqa.selenium.chromium.ChromiumDriver.lambda$new$3 Unable to find version of CDP to use for . You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.1.0` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.
解决办法
1. 严格匹配ChromeDriver与Chrome浏览器版本
日志明确提示ChromeDriver 102未适配Chrome 103,这是核心问题:
- 查看类Unix系统的Chrome版本:执行命令
google-chrome --version(Mac系统可使用google-chrome-stable --version) - 下载与Chrome版本完全一致的ChromeDriver,替换项目中现有驱动文件
- 给ChromeDriver添加可执行权限:执行
chmod +x chromedriver
2. 添加对应版本的CDP依赖
针对日志中“找不到CDP 103实现”的警告,需补充匹配的Selenium DevTools依赖:
- 若使用Maven,在
pom.xml中添加对应依赖,示例:<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-devtools-v103</artifactId> <version>你的Selenium版本号</version> </dependency> - 注意:
v103需和Chrome版本一致,依赖版本要与项目中Selenium的版本完全匹配
3. 配置Chrome无界面运行参数(服务器环境)
CentOS7等服务器环境无图形界面,需添加Chrome启动参数避免运行异常:
ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new"); options.addArguments("--no-sandbox"); options.addArguments("--disable-dev-shm-usage"); ChromeDriver driver = new ChromeDriver(options);
4. 排查远程会话转发问题
Forwarding newSession on session null to remote一般是版本不匹配导致的衍生问题,解决版本匹配后大概率消失;若仍存在:
- 检查系统防火墙、代理是否阻止了ChromeDriver的本地连接
- 确认Selenium代码未误配置为远程WebDriver模式,确保是本地启动ChromeDriver
内容的提问来源于stack exchange,提问作者ali reza
相关产品推荐
相关产品推荐

