下面的代码我尝试使用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
类不支持无头模式。请引导。
已标记为工作的解决方案可以在此处找到:使用ChromeDriver和无头模式下载Java、Selenium中的文件
试试这个,并在driver.get(pdfUrl);
之后删除代码
这可能会帮助您使用字符串的HashMap和对象,
<--语言:lang java-->
HashMap<String, Object> prefs = new HashMap<>();
prefs.put("plugins.always_open_pdf_externally", true);
ChromeOptions.setExperimentalOption("prefs", prefs);
我在参考以下链接后得到了解决方案:使用ChromeDriver和无头模式下载Java、Selenium中的文件
以下是在无头模式下下载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);
}