ChromeDriver(Capabilities 功能)已弃用

我使用 ChromeDriver 2.33WebDriver 3.6.0 并尝试设置文件下载的默认目录.

I use ChromeDriver 2.33 with WebDriver 3.6.0 and try to set default directory for file download.

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", Vars.DOWNLOAD_FOLDER_ROOT);
DesiredCapabilities caps = DesiredCapabilities.chrome();

ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setExperimentalOption("prefs", prefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(caps);

我在文档中找到了这个:

I found this in docs:

改用 ChromeDriver(ChromeOptions).创建一个新的 ChromeDriver实例.这些功能将传递给 chromedriver 服务.

Use ChromeDriver(ChromeOptions) instead. Creates a new ChromeDriver instance. The capabilities will be passed to the chromedriver service.

推荐答案

我希望您想询问解决方法以避免弃用.

I hope you wanted to ask about the workaround to avoid the deprecation.

仅使用 Capabilities 构建的旧方法已被弃用.现在,它需要一个 ChromeDriverService &Capabilities 作为参数.因此,只需构建一个 ChromeDriverService 并将其与您的 Capabilities 一起传递即可删除弃用警告.

The old method of just building with Capabilities is deprecated. Now, it takes a ChromeDriverService & Capabilities as parameters. So, just a build a ChromeDriverService and pass the same along with your Capabilities to remove the deprecation warning.

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

ChromeDriverService service = new ChromeDriverService.Builder()
                    .usingDriverExecutable(new File("/usr/local/chromedriver"))
                    .usingAnyFreePort()
                    .build();
ChromeDriver driver = new ChromeDriver(service, capabilities);

由于 ChromeDriver(service, capabilities) 现在也已弃用,您可以使用,

Since ChromeDriver(service, capabilities) is deprecated now as well, you can use,

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

ChromeDriverService service = new ChromeDriverService.Builder()
                            .usingDriverExecutable(new File("/usr/local/chromedriver"))
                            .usingAnyFreePort()
                            .build();
ChromeOptions options = new ChromeOptions();
options.merge(capabilities);    
ChromeDriver driver = new ChromeDriver(service, options);

但是,您可以完全跳过 DesiredCapabilities 并仅将 ChromeOptionssetCapability 方法一起使用,

However, You can completely skip DesiredCapabilities and use only ChromeOptions with setCapability method like,

ChromeOptions options = new ChromeOptions();
options.setCapability("capability_name", "capability_value");
driver = new ChromeDriver(options);

相关文章