Selenium 4.3.0 Firefox遗留功能报错求助(自定义BrowserFactory)
解决Selenium 4.3.0 Firefox测试的Legacy Capabilities警告问题
问题背景
升级到Selenium 4.3.0后,在Jenkins上运行Firefox测试时触发以下警告:
WARNING: Support for Legacy Capabilities is deprecated; You are sending the following invalid capabilities: [applicationName, firefox_profile];
原因是代码中使用了Selenium 4已废弃的旧版能力配置,需要迁移到W3C标准语法。以下是针对自定义BrowserFactory类的具体修改方案:
核心修改点
1. 废弃FirefoxProfile,改用FirefoxOptions直接配置偏好
Selenium 4+中FirefoxProfile属于旧版协议能力,W3C模式下应直接通过FirefoxOptions.addPreference()设置浏览器偏好,无需单独创建FirefoxProfile实例。
2. 替换applicationName为se:applicationName
旧的applicationName是非标准能力,W3C标准下需要使用带命名空间的se:applicationName来指定。
修改后的完整代码
修复后的getFirefoxDriver()方法
private static WebDriver getFirefoxDriver() { if (SELENIUM_GRID) { FirefoxOptions options = new FirefoxOptions(); // 直接通过FirefoxOptions添加原FirefoxProfile中的偏好设置 options.addPreference("intl.accept_languages", LOCALE); options.addPreference("browser.helperApps.neverAsk.saveToDisk","application/msword"); options.addPreference("network.proxy.type",1); options.addPreference("network.proxy.http","isp-ceg.emea.cegedim.grp"); options.addPreference("network.proxy.ssl","isp-ceg.emea.cegedim.grp"); options.addPreference("network.proxy.ftp","isp-ceg.emea.cegedim.grp"); options.addPreference("network.proxy.http_port",3128); options.addPreference("network.proxy.ssl_port",3128); options.addPreference("network.proxy.ftp_port",3128); options.addPreference("print.always_print_silent", true); options.addPreference("network.proxy.share_proxy_settings",true); options.addPreference("network.proxy.no_proxies_on","localhost,127.0.0.1,*.cegedim.com,*.cegedim.cloud,*.cegedim.grp,*.cegedim.hds,*.cegedim.clt,*.monlogicielmedical.com"); // 替换applicationName为W3C标准的se:applicationName if(SELENIUM_GRID_DEBUG) { options.setCapability("se:applicationName", DEBUG_FIREFOX); } else { options.setCapability("se:applicationName", NO_DEBUG); } RemoteWebDriver driver = new RemoteWebDriver(SELENIUM_GRID_URL, options); driver.setFileDetector(new LocalFileDetector()); if(SELENIUM_GRID_DEBUG) { driver.manage().window().maximize(); logger.info("driver maximized"); } return driver; } else { WebDriver webDriver ; WebDriverManager.firefoxdriver().setup(); FirefoxOptions options = new FirefoxOptions() .addPreference("intl.accept_languages", LOCALE) .addPreference("browser.helperApps.neverAsk.saveToDisk","application/msword") .addPreference("print.always_print_silent", true) .addPreference("print.show_print_progress", false); // 移除原setProfile(new FirefoxProfile()),无需单独创建空Profile webDriver = new FirefoxDriver(options); webDriver.manage().window().maximize(); return webDriver; } }
同步修复ChromeDriver中的applicationName问题
原getChromeDriver()方法中同样使用了旧版applicationName,一并修改:
private static WebDriver getChromeDriver() { ChromeOptions options = new ChromeOptions(); options.addArguments("--lang=" + LOCALE); if (SELENIUM_GRID) { // 移除冗余的options.setCapability(ChromeOptions.CAPABILITY, options); // 替换applicationName为se:applicationName if(SELENIUM_GRID_DEBUG) { options.setCapability("se:applicationName", DEBUG); } else { options.setCapability("se:applicationName", NO_DEBUG); } RemoteWebDriver driver = new RemoteWebDriver(SELENIUM_GRID_URL, options); driver.setFileDetector(new LocalFileDetector()); if(SELENIUM_GRID_DEBUG) { Waiter.sleep(1500); driver.manage().window().maximize(); } else { Waiter.sleep(1500); driver.manage().window().setSize(new Dimension(1920,1080)); } return driver; } WebDriver driver = new ChromeDriver(options); driver.manage().window().maximize(); return driver; }
修改说明
- 移除了所有
FirefoxProfile实例的创建,直接通过FirefoxOptions.addPreference()配置浏览器参数,符合W3C标准。 - 将所有
applicationName能力替换为se:applicationName,这是Selenium Grid支持的W3C标准扩展能力。 - 修复了
getChromeDriver()方法中缺失的return语句和冗余的配置代码。
内容的提问来源于stack exchange,提问作者Ahmad Ghalwash
相关产品推荐
相关产品推荐

