ChromeDriver(Capabilities 能力) 已弃用

ChromeDriver(Capabilities capabilities) is deprecated

我使用 ChromeDriver 2.33WebDriver 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);

我在文档中找到了这个:

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

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

仅使用 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);

编辑: 由于 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);

但是,您可以完全跳过 DesiredCapabilities 并仅使用 ChromeOptionssetCapability 方法,例如,

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

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

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