Selenium+Firefox+Geckodriver测试失败:Failed to decode response from marionette
精准匹配版本组合:Selenium 3.141.59是2018年的旧版本,对Firefox 119这类高版本浏览器支持有限。如果坚持用Selenium 3.x,建议将Firefox降级到90.x-100.x区间(该区间与Selenium 3.141.59兼容性更稳定),同时搭配对应版本的geckodriver(比如geckodriver 0.30.0对应Firefox 90+)。如果允许升级Selenium,直接升级到Selenium 4.10+,它对Firefox 119和geckodriver 0.33.0的支持更完善。
检查geckodriver配置:确保geckodriver的路径正确配置:
- 要么将geckodriver所在目录添加到系统环境变量
PATH中; - 要么在代码中显式指定路径:
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
注意路径不要包含空格,若有空格需用双引号包裹或使用短路径。
- 要么将geckodriver所在目录添加到系统环境变量
添加Firefox启动参数:通过
FirefoxOptions添加参数避免进程异常终止:FirefoxOptions options = new FirefoxOptions(); options.addArguments("--no-sandbox"); // 禁用沙箱,避免权限问题 options.addArguments("--disable-gpu"); // 禁用GPU加速,解决部分环境兼容性问题 options.addArguments("--disable-extensions"); // 禁用所有扩展,排除干扰 options.addArguments("--marionette-port", "2828"); // 显式指定marionette端口,避免端口冲突 WebDriver driver = new FirefoxDriver(options);排查系统拦截:Windows防火墙、杀毒软件可能会拦截geckodriver或Firefox的进程通信,导致marionette无法正常响应。暂时关闭杀毒软件,或把geckodriver.exe和Firefox.exe添加到信任列表后重试。
使用全新Firefox配置文件:旧的Firefox配置文件可能存在缓存或冲突,让Selenium使用临时新profile:
FirefoxOptions options = new FirefoxOptions(); options.addArguments("-profile", "C:\\temp\\selenium_firefox_profile"); // 指定空目录作为新profile WebDriver driver = new FirefoxDriver(options);运行后会自动生成干净的配置文件,避免旧数据干扰。
检查Java版本兼容性:Selenium 3.141.59推荐使用Java 8或Java 11,如果当前使用Java 17及以上版本,可能存在兼容性问题,切换到推荐版本重试。
内容的提问来源于stack exchange,提问作者Akash Chaturvedi

