fmichail
asked on
How to make a link download file instead of print it
I am trying to download a PDF file from a site using Selenium (using vb.net). Unfortunately, the file link is made to open a print dialog not downloading directly. I got 2 links from the same site, first one download successfully, but the second one opens a print dialog (which is not what I want).
Here are the 2 elements xPath
First One (download OK)
<a id="aAppleWallet" href="/Visa/VisaPass/5D276A35-CFA2-454F-84D8-B6EAC7051B0C" class="action_icon" title="Add to Apple Wallet" style="display: none;">
<span class="icon appleWallet"></span>
</a>
The second one (Opens print dialog)
<a href="https://onlineservices.bupa.com.sa/Visitor/Econtract.aspx?dR7fRY/tbxJpA3ZN+luuk72Bpl2NxpXf" target="_blank" class="action_icon" title="Download Insurance">
<span class="icon downloadfile"></span>
</a>
How can I make the second link download the file instead of printing it (both files are PDF).
Your help is appreciated. Thanks
ASKER
Hi Xujin
I tried adding the attribute 'download' but the link is still opening the print dialog instead of just downloading
Here is my code in vb.net
theDownloadVisa = driver.FindElement(By.XPath("/html/body/div[1]/div[1]/div[1]/section[2]/div[1]/div[4]/div[4]/div[2]/div/table/tbody/tr[" & CStr(z) & "]/td[8]/a[2]"))
If theDownloadVisa IsNot Nothing Then
Dim downloadfileName As String = theDownloadVisa.GetAttribute("href")
driver.ExecuteScript("arguments[0].setAttribute('download','" & downloadfileName & "');", theDownloadVisa)
theDownloadVisa.Click()
end if
what Am I doing wrong?
I tried adding the attribute 'download' but the link is still opening the print dialog instead of just downloading
Here is my code in vb.net
theDownloadVisa = driver.FindElement(By.XPath("/html/body/div[1]/div[1]/div[1]/section[2]/div[1]/div[4]/div[4]/div[2]/div/table/tbody/tr[" & CStr(z) & "]/td[8]/a[2]"))
If theDownloadVisa IsNot Nothing Then
Dim downloadfileName As String = theDownloadVisa.GetAttribute("href")
driver.ExecuteScript("arguments[0].setAttribute('download','" & downloadfileName & "');", theDownloadVisa)
theDownloadVisa.Click()
end if
what Am I doing wrong?
The website https://onlineservices.bupa.com.sa/Visitor/Econtract.aspx?dR7fRY/tbxJpA3ZN+luuk72Bpl2NxpXf
has onload='window.print()'
It is not a pdf
Unless you can remove or overwrite the print, you will have to dismiss the dialog
has onload='window.print()'
It is not a pdf
Unless you can remove or overwrite the print, you will have to dismiss the dialog
ASKER
Hi Michel,
If that is the case, then there is no browser way to override this event...Correct?
If that is the case, then there is no browser way to override this event...Correct?
Likely not.
You can use a proxy
You can use a proxy
Since you have an HTML page, you can't download it. If you can convert the HTML page to PDF and upload it to the server, then the download will work. Also for the download attribute to work, link needs to be in the same domain.
ASKER
Michel,
What do you mean by using the proxy?
Michel and Yujin,
Remember the domain that I download from is not under my control. I am just using 2 links in it to automate the downloading of the files that the site exposes. Is the use of proxy still an option?
What do you mean by using the proxy?
Michel and Yujin,
Remember the domain that I download from is not under my control. I am just using 2 links in it to automate the downloading of the files that the site exposes. Is the use of proxy still an option?
If it is a third-party site, that provides HTML and you need to provide a PDF file to your site visitor, you may better off making a PHP script, that reads this web page, converts it to PDF, makes it available for download.
ASKER
Dear Yujin,
I am not using PHP, I use ASP.NET however, the application that I download from is a desktop application using VB.NET. Can you send me a code to read a webpage and convert it to pdf? I am not good at all in this kind of HTML script. I can use this code in my website using ASP.NET to create a pdf on my domain then I download using the desktop application. Do you think that would work, If you can send me this code, I believe that will solve my problem unless I am missing something?
Thanks
I am not using PHP, I use ASP.NET however, the application that I download from is a desktop application using VB.NET. Can you send me a code to read a webpage and convert it to pdf? I am not good at all in this kind of HTML script. I can use this code in my website using ASP.NET to create a pdf on my domain then I download using the desktop application. Do you think that would work, If you can send me this code, I believe that will solve my problem unless I am missing something?
Thanks
So what Yujin suggested IS a proxy or in this case a scraper
https://www.google.com/search?q=vb.net+scrape+web+page
https://www.google.com/search?q=vb.net+scrape+web+page
ASKER
Hi Yujin
Sorry, I got nowhere with the scraper links, it is a new concept to me, and I feel very hard to proceed, Is there an easy guide for me?
Thanks
Sorry, I got nowhere with the scraper links, it is a new concept to me, and I feel very hard to proceed, Is there an easy guide for me?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
Refer
https://www.samanthaming.com/tidbits/38-html-download-attribute/