Java ChromeDriver(Capabilities capabilities) 已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46786043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:28:41  来源:igfitidea点击:

ChromeDriver(Capabilities capabilities) is deprecated

javaselenium-webdriverselenium-chromedriverdesiredcapabilities

提问by plaidshirt

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

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

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:

我在文档中找到了这个:

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

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

采纳答案by Sridhar

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

我希望您想询问避免弃用的解决方法。

The old method of just building with Capabilitiesis deprecated. Now, it takes a ChromeDriverService& Capabilitiesas parameters. So, just a build a ChromeDriverServiceand pass the same along with your Capabilitiesto remove the deprecation warning.

Capabilities不推荐使用仅构建的旧方法。现在,它需要一个ChromeDriverService&Capabilities作为参数。因此,只需构建一个ChromeDriverService并传递相同的内容Capabilities即可删除弃用警告。

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

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

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

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

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);

However, You can completely skip DesiredCapabilitiesand use only ChromeOptionswith setCapabilitymethod like,

但是,您可以完全跳过DesiredCapabilities并仅ChromeOptions使用setCapability类似的方法,

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

回答by Jean-Fran?ois Gagnon

The new way to use chrome capabilities is like this :

使用 chrome 功能的新方法是这样的:

ChromeOptions options = new ChromeOptions();
    // Proxy proxy = new Proxy();
    // proxy.setHttpProxy("myhttpproxy:3337");
    // options.setCapability("proxy", proxy);
    // options.addArguments("--headless");
    // options.addArguments("--disable-gpu");
    // options.setAcceptInsecureCerts(true);
    // options.addArguments("--allow-insecure-localhost");
    // options.addArguments("--lang=fr-CA");
    options.addArguments("--start-maximized");
driver = new ChromeDriver(options);

You can get more options by looking at this site : https://sites.google.com/a/chromium.org/chromedriver/capabilities

您可以通过查看此站点获得更多选项:https: //sites.google.com/a/chromium.org/chromedriver/capabilities