Java谷歌无头模式速度_Java selenium无头模式下载CFM文件问题

本文探讨了使用Java Selenium在无头模式下下载PDF文件时遇到的问题,特别是针对CFM文件的下载。通过设置ChromeOptions和实验性选项,尝试解决下载行为,但未成功。代码示例展示了配置过程,包括设置下载路径、禁用扩展等。目前,尽管没有出现异常,但CFM文件的下载仍未能实现。
摘要由CSDN通过智能技术生成

在下面的代码中,我尝试在无头模式下使用selenium从chrome浏览器下载PDF。它对PDF文件很好,但对

.cfm

文件。

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 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/formPDF.cfm";

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 commandParams = new HashMap<>();

commandParams.put("cmd", "Page.setDownloadBehavior");

Map 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", "text/x-cfm");

request.setEntity(new StringEntity(command));

httpClient.execute(request);

// Opens pdf of specific URL

driver.get(pdfUrl);

}

我也提到了下面的答案,但没有起作用。我也没有在控制台中得到任何异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值