当我点击chrome中的按钮时,它会下载一个jpg文件。 但是当我使用phantomjs时,它不会下载jpg,也不会出现任何错误。
示例:https://deepak5j.github.io/HelloProjectPage/download.html
如何使用phantomjs下载文件?
答案 0 :(得分:0)
var casper = require("casper").create();
casper.start('https://deepak5j.github.io/HelloProjectPage/download.html', function() {
var url = 'https://raw.githubusercontent.com/Deepak5j/WebImages/master/Tiles/';
this.download(url, 'sun_tile.jpg');
});
casper.run(function() {
this.exit();
});
答案 1 :(得分:0)
使用幻像按钮单击不会下载文件但单击后您可以将文件作为输入流。因此,您可以将输入流保存到文件。下面的代码将给出一个想法:
driver.findElement(By.className("Download")).click();
List<LogEntry> harLogEntries = driver.manage().logs().get("har").getAll();
LogEntry lastLogEntry = harLogEntries.get(harLogEntries.size() - 1);
String lastRequestUrl = getRequestUrlFromHarLogEntry(lastLogEntry);
DefaultHttpClient client;
HttpResponse response;
HttpGet get = new HttpGet(lastRequestUrl);
response = client.execute(get);
InputStream dataStream = response.getEntity().getContent();
我将在下面分享getRequestUrlFromHarLogEntry:
private String getRequestUrlFromHarLogEntry(LogEntry logEntry)
throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> message = objectMapper.readValue(logEntry.getMessage(), Map.class);
Map<String, Object> log = (Map<String, Object>) message.get("log");
List<Object> entries = (List<Object>) log.get("entries");
Map<String, Object> lastEntry = (Map<String, Object>) entries.get(entries.size() - 1);
Map<String, Object> request = (Map<String, Object>) lastEntry.get("request");
String url = (String) request.get("url");
return url;
}
希望这会有所帮助..