<small id='1boOv'></small><noframes id='1boOv'>

<legend id='1boOv'><style id='1boOv'><dir id='1boOv'><q id='1boOv'></q></dir></style></legend>
    • <bdo id='1boOv'></bdo><ul id='1boOv'></ul>

      1. <tfoot id='1boOv'></tfoot>
        <i id='1boOv'><tr id='1boOv'><dt id='1boOv'><q id='1boOv'><span id='1boOv'><b id='1boOv'><form id='1boOv'><ins id='1boOv'></ins><ul id='1boOv'></ul><sub id='1boOv'></sub></form><legend id='1boOv'></legend><bdo id='1boOv'><pre id='1boOv'><center id='1boOv'></center></pre></bdo></b><th id='1boOv'></th></span></q></dt></tr></i><div id='1boOv'><tfoot id='1boOv'></tfoot><dl id='1boOv'><fieldset id='1boOv'></fieldset></dl></div>
      2. 使用 ChromeDriver 和无头模式下载 Java、Selenium 文件

        时间:2023-06-27
        <i id='sWomv'><tr id='sWomv'><dt id='sWomv'><q id='sWomv'><span id='sWomv'><b id='sWomv'><form id='sWomv'><ins id='sWomv'></ins><ul id='sWomv'></ul><sub id='sWomv'></sub></form><legend id='sWomv'></legend><bdo id='sWomv'><pre id='sWomv'><center id='sWomv'></center></pre></bdo></b><th id='sWomv'></th></span></q></dt></tr></i><div id='sWomv'><tfoot id='sWomv'></tfoot><dl id='sWomv'><fieldset id='sWomv'></fieldset></dl></div>
          <tbody id='sWomv'></tbody>

          1. <small id='sWomv'></small><noframes id='sWomv'>

          2. <legend id='sWomv'><style id='sWomv'><dir id='sWomv'><q id='sWomv'></q></dir></style></legend>
              <bdo id='sWomv'></bdo><ul id='sWomv'></ul>
              <tfoot id='sWomv'></tfoot>

                • 本文介绍了使用 ChromeDriver 和无头模式下载 Java、Selenium 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  由于我仍然不清楚如何在 ChromeDriver - selenium [Java] 中使用 --headless 模式下载文件,请在此处添加如何执行此操作的示例,我尝试这样做(文件下载没有 --headless 选项也能正常工作):

                  As it is still not clear for me how to download files using --headless mode in ChromeDriver - selenium [Java], add here please the example of how to do so, I try to do it like that (the file downloading works properly without --headless option):

                  ChromeOptions lChromeOptions = new ChromeOptions();
                  HashMap<String, Object> lChromePrefs = new HashMap<String, Object>();
                  lChromePrefs.put("profile.default_content_settings.popups", 0);
                  lChromePrefs.put("download.default_directory", _PATH_TO_DOWNLOAD_DIR);
                  lChromePrefs.put("browser.set_download_behavior", "{ behavior: 'allow' , downloadPath: '"+_PATH_TO_DOWNLOAD_DIR+"'}");
                  
                  lChromeOptions.addArguments("--headless");
                  lChromeOptions.addArguments("--disable-gpu");
                  lChromeOptions.setExperimentalOption("prefs", lChromePrefs);
                  WebDriver lWebDriver = new ChromeDriver(lChromeOptions);
                  

                  据我所知,自 Chrome v60+ 起,通过设置 Browser.setDownloadBehaviour(true, _DIRECTORY) 应该可以在无头模式下下载文件,但我找不到 ChromeDriver 是否已经支持它的信息.只有我使用错误的 chrome 首选项作为参数

                  From what I know, downloading files in headless mode should be possible since Chrome v60+ by setting Browser.setDownloadBehaviour(true, _DIRECTORY) but I cant find the information whether ChromeDriver already supports it or it is just me using wrong chrome preferences as arguments

                  ChromeDriver 版本:2.34Selenium + WebDriver 版本:3.8.1

                  ChromeDriver version: 2.34 Selenium + WebDriver version: 3.8.1

                  推荐答案

                  在Java中这样使用:

                  In Java use like this :

                  System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
                           ChromeOptions options = new ChromeOptions();
                                  options.addArguments("--test-type");
                                  options.addArguments("--headless");
                                  options.addArguments("--disable-extensions"); //to disable browser extension popup
                  
                                  ChromeDriverService driverService = ChromeDriverService.createDefaultService();
                                  ChromeDriver driver = new ChromeDriver(driverService, options);
                  
                                  Map<String, Object> commandParams = new HashMap<>();
                                  commandParams.put("cmd", "Page.setDownloadBehavior");
                                  Map<String, String> params = new HashMap<>();
                                  params.put("behavior", "allow");
                                  params.put("downloadPath", "//home//vaibhav//Desktop");
                                  commandParams.put("params", params);
                                  ObjectMapper objectMapper = new ObjectMapper();
                                  HttpClient httpClient = HttpClientBuilder.create().build();
                                  String command = objectMapper.writeValueAsString(commandParams);
                                  String u = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
                                  HttpPost request = new HttpPost(u);
                                  request.addHeader("content-type", "application/json");
                                  request.setEntity(new StringEntity(command));
                                  httpClient.execute(request);
                          driver.get("http://www.seleniumhq.org/download/");
                          driver.findElement(By.linkText("32 bit Windows IE")).click();
                  

                  这篇关于使用 ChromeDriver 和无头模式下载 Java、Selenium 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从渲染器接收消息超时 下一篇:Java 和 Selenium:页面对象中的静态方法

                  相关文章

                  <legend id='uw0kz'><style id='uw0kz'><dir id='uw0kz'><q id='uw0kz'></q></dir></style></legend>

                    <small id='uw0kz'></small><noframes id='uw0kz'>

                      • <bdo id='uw0kz'></bdo><ul id='uw0kz'></ul>

                    1. <i id='uw0kz'><tr id='uw0kz'><dt id='uw0kz'><q id='uw0kz'><span id='uw0kz'><b id='uw0kz'><form id='uw0kz'><ins id='uw0kz'></ins><ul id='uw0kz'></ul><sub id='uw0kz'></sub></form><legend id='uw0kz'></legend><bdo id='uw0kz'><pre id='uw0kz'><center id='uw0kz'></center></pre></bdo></b><th id='uw0kz'></th></span></q></dt></tr></i><div id='uw0kz'><tfoot id='uw0kz'></tfoot><dl id='uw0kz'><fieldset id='uw0kz'></fieldset></dl></div>
                    2. <tfoot id='uw0kz'></tfoot>