Java selenium headless mode下载PDF问题

Java selenium headless mode download PDF issue

下面的代码我尝试使用 selenium 从 chrome 浏览器下载 PDF。

public static void main(String args[]) throws InterruptedException, AWTException, IOException, DocumentException {

    System.setProperty("webdriver.chrome.driver", "/home/sejalj/OtherProj/webDrivers/chromedriver_64");
    ChromeOptions ChromeOptions = new ChromeOptions();
    ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox");

    WebDriver driver = new ChromeDriver(ChromeOptions);

    String baseUrl = "http://url.com/";

    driver.get(baseUrl);

    driver.findElement(By.name("wl_user_name")).sendKeys("uname");
    driver.findElement(By.name("wl_user_password")).sendKeys("password");
    driver.findElement(By.cssSelector("input[value=Login]")).click();

    String pdfUrl = "https://www.pdfurl.com/displayImageDocs.php?
    f=MjAxODA3MjQxMDUwNzA4Ni5QREY=&p=aW1hZ2UuaW1hZ2ViYW5rLmJsdsaddhbmsxLjIwMTgwNy4yMDE4MDcyNA==&a=MTAwMjM0&POL_NUM=AAS06036999";";

    // Opens pdf of specific URL
    driver.get(pdfUrl);     

    Actions a = new Actions(driver);

    // To press CTRL+S
    a.keyUp(Keys.CONTROL).sendKeys("s").build().perform();

    Robot robot = new java.awt.Robot();     
    robot.keyPress(KeyEvent.VK_BACK_SPACE);     
    robot.keyRelease(KeyEvent.VK_BACK_SPACE);
    int keyInput[] = {KeyEvent.VK_A, KeyEvent.VK_A, KeyEvent.VK_A, KeyEvent.VK_A, KeyEvent.VK_UNDERSCORE,
                      KeyEvent.VK_P, KeyEvent.VK_O, KeyEvent.VK_L, KeyEvent.VK_I, KeyEvent.VK_C, KeyEvent.VK_Y
                     };

    for (int i = 0; i < keyInput.length; i++) {
          robot.keyPress(keyInput[i]);
          robot.keyRelease(keyInput[i]);
    }

    // To press ENTER
    robot.keyPress(KeyEvent.VK_ENTER);
    Thread.sleep(1000);
    robot.keyRelease(KeyEvent.VK_ENTER);

    System.out.println("Preparing Policies document...Please wait");
    System.out.println("Document prepared..");
}

以上代码在 chrome 运行 没有 --headless 模式时工作正常。 但是在 --headless 模式下上面的代码不起作用。

Robot class 不支持无头模式。 请指导。

Solution that has been mark as working can be found here: Download files in Java, Selenium using ChromeDriver and headless mode

试试这个并删除 driver.get(pdfUrl);
之后的代码 这可能会帮助您将字符串的 HashMap 与对象一起使用,

<-- 语言:lang-java -->

HashMap<String, Object> prefs = new HashMap<>();        
prefs.put("plugins.always_open_pdf_externally", true);
ChromeOptions.setExperimentalOption("prefs", prefs);

参考下面link我得到了解决方案: Download files in Java, Selenium using ChromeDriver and headless mode

这是在无头模式下下载 PDF 文件的工作代码。

public static void main(String args[]) throws InterruptedException, AWTException, IOException, DocumentException {

    System.setProperty("webdriver.chrome.driver", "/home/OtherProj/webDrivers/chromedriver_64");
    
    String downloadPath = "/home/Downloads/AAAA/";
    File file = new File(downloadPath);
    if(!file.exists())
        file.mkdirs();
    
    ChromeOptions chromeOptions = new ChromeOptions();
    
    HashMap<String, Object> prefs = new HashMap<>();        
    prefs.put("plugins.always_open_pdf_externally", true);

    chromeOptions.addArguments("--test-type");
    chromeOptions.addArguments("--disable-extensions");
    chromeOptions.setExperimentalOption("prefs", prefs);
    chromeOptions.setHeadless(true);
    
    String pdfUrl = "https://www.dummyurl.com/prod/displayImageDocs.php?f=MjAxODsafdfgsdjhgsjkA3MjQxMDUwNzA4Ni5QREY=&p=aW1hZ2UuaW1hZ2ViYW5rLmJhbmsxLjIwMTgwNy4yMDE4MDcyNA==&a=MTAwMsgsdjM0&POL_NUM=AAS06036999";
    
    ChromeDriverService driverService = ChromeDriverService.createDefaultService();
    WebDriver driver = new ChromeDriver(driverService, chromeOptions);

    // Saves the file on the given path
    PDFDemo.downloadFile(downloadPath, driverService, driver, pdfUrl);
    logger.debug("File Downloded Start");
        
    long fileSizeOne;
    long fileSizeTwo;

    do {

           File downlodedFolder = new File(downloadPath);
           File downlodedFile = downlodedFolder.listFiles()[0];

           fileSizeOne = downlodedFile.length();
           logger.debug("fileSizeOne " + fileSizeOne);
           Thread.sleep(1500);
           fileSizeTwo = downlodedFile.length();
           logger.debug("fileSizeTwo " + fileSizeTwo);
    } while (fileSizeTwo != fileSizeOne);

    logger.debug("File Downloded Completed");       
    System.out.println("Document Downloaded..");
}

private static void downloadFile(String downloadPath, ChromeDriverService driverService, WebDriver driver, String pdfUrl) throws ClientProtocolException, IOException, InterruptedException {
    
    Map<String, Object> commandParams = new HashMap<>();
    commandParams.put("cmd", "Page.setDownloadBehavior");
    Map<String, String> params = new HashMap<>();
    params.put("behavior", "allow");
    params.put("downloadPath", downloadPath);
    commandParams.put("params", params);
    HttpClient httpClient = HttpClientBuilder.create().build();

    ObjectMapper objectMapper = new ObjectMapper();
    String command = objectMapper.writeValueAsString(commandParams);        
    String u = driverService.getUrl().toString() + "/session/" + ((RemoteWebDriver) driver).getSessionId() + "/chromium/send_command";
    HttpPost request = new HttpPost(u);
    request.addHeader("content-type", "application/pdf");
    request.setEntity(new StringEntity(command));
    httpClient.execute(request);

    // Opens pdf of specific URL
    driver.get(pdfUrl);
}