I need to automate download of a pdf file. Sample link below
<a href="https:\\..?serachid=1"
onclick="window.open(this.href,'popupwindow','scrollbars=no,resizable=no')return false" >click to
download</a>
Link's href doesn't have .pdf at the end. on clicking on the link, I am getting IE popup which has embedded pdf. clicking on print from popup makes a processing window which takes quit a lot of time and we are unable to track the progress. Rather clicking on save a copy button ( from flier which appears below works fine) if I click it manually, but I couldn't find a way to click on same from code.)
I am looking for option to right click and clicking on "save target as" or option to click the "save a copy" buttonenter image description here I have tried quit a lot of suggestions still I am unable to find a solution which works for me, please help.
Note: I can't modify html. and I can't use selenium.
[Edit] I have tried to add download attribute as follows, still am getting IE popup, not direct download option as expected.
<a href="https:\\..?serachid=1"
onclick="window.open(this.href,'popupwindow','scrollbars=no,resizable=no')return false" download="">click to
download</a>
IE doesn't support download
attribute in <a>
so it doesn't work in IE. You can check the browser compatibility table.
For IE, if you want to click the link to download, you can only convert the pdf file to blob data then download it using msSaveBlob
. msSaveBlob is IE's own API for creating and downloading files. You can refer to the code below:
<a href="your_file_link" onclick="downloadpdf();return false">click to download</a>
<script>
function downloadpdf() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
//console.log(this.response, typeof this.response);
window.navigator.msSaveBlob(this.response, 'testpdf.pdf');
}
}
xhr.open('GET', 'your_file_link');
xhr.responseType = 'blob';
xhr.send();
}
</script>