Java硒无头模式下载PDF期刊

时间:2018-10-29 05:43:47

标签: java selenium selenium-chromedriver headless awtrobot

以下代码中,我尝试使用chrome从chrome浏览器下载PDF。

Dashboard

当chrome在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.."); } 模式下运行时,以上代码可以正常工作。 但是在--headless模式下,以上代码无法正常工作。

--headless类在无头模式下不支持。 请指导。

  

在此处可以找到被标记为有效的解决方案:   Download files in Java, Selenium using ChromeDriver and headless mode

2 个答案:

答案 0 :(得分:0)

尝试此操作并在driver.get(pdfUrl);之后删除代码
         这可以帮助您将字符串的HashMap与对象配合使用,

<-语言:lang-java->

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

答案 1 :(得分:0)

引用以下链接后,我得到了解决方案: 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);      
    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);
}