Is it possible to download PDF through PDF viewer using selenium C# - javascript

My problem Statement is:
I want to download PDF using JavaScriptExecutor, When I click on the
PDF link it opens the PDF in a new window, It is an online PDF Viewer window, having a toolbar section where print, download..etc, features are available, Manually I can download by clicking on Download button. Now I want to automate this Scenario, I Researched and found that it can be
handled by using "javascript executor" I inspect that opened PDF window and the toolbar section is inside nested shadow root. Then I proceeded with writing the JSPath of the download element, basically, I copied the JSPath of the download Element, I checked it on the Console and it was able and locate and perform the Click operation. Same when I tried to do it through Script it gives me the exception "Cannot read properties of null(reading 'ShadowRoot')"
Screenshots of DevTool Console
The code I used so far:
IWebDriver driver;
driver.FindElement(By.Xpath("Xpath_of_PDF_Link")).Click();
driver.SwitchTo().Window(driver.WindowHandles.Last());
Thread.Sleep(2000);
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
IWebElement downloadButton = (IWebElement)js.ExecuteScript("return document.querySelector(\"#viewer\").shadowRoot.querySelector(\"#toolbar\").shadowRoot.querySelector(\"#downloads\").shadowRoot.querySelector(\"#download\")");
Thread.Sleep(1000);
var element = "argument[0].click()";
js.ExecuteScript(element,downloadButton);
Screenshots of devTool attached below
Does anyone have any possible solutions or insight into How to download the PDF? I'm relatively new to programming and would appreciate any advice.
Please let me know, If any other information required.. Thanks in advance

You could just get the url of the pdf link, then download it using an HTTP client.
I'm not familiar with C#, but here's pseudocode:
var url = driver.findElement(pdflink).getAttribute(href);
var client = new HttpClient();
var result = client.Get(url);
Files.write("myfile.pdf", pdf);

Related

Js code to open base64 pdf is failing in chrome for pdf of size > 1.5MB

I have my JS code link.href = 'data:application/pdf;base64,' + data; where data is base64 string which opens properly in firefox but not in chrome when size of pdf file is huge > 1.5MB. Can someone provide solution to handle this issue.
Chrome doesn't allow opening data URLs for security reasons. You might be able to open the link using "Open in new tab" (right click menu).
As a work around, if possible, you can force the user to download the file using <a download="example.pdf" href= data:URL >
Or try using blob:URL instead of data:URL. This might help.

Apps Script: open PDF right after creation

Is it possible to open a PDF file right after its creation with Apps Script in Google Sheets?
My script is generating a pdf based on a Google Sheet and saving it on Google Drive. I would love to be able to automatically open it to print it.
Would spare some time for the user, not having to find the file and open it manually.
Thanks a lot for your help :)
Well, if you are opening just it to print it; Most modern printers can be WiFi connected with custom email addresses. Then you can just email the file as an attachment to the printer.
It's not the perfect solution but you could present them with a dialog that has a Url to the file. If you right click on it you'll get a menu that gives you the option to open in a new tab or a new window.
function createPDFofSheet() {
var ss=SpreadsheetApp.getActive();
var file=DriveApp.createFile(ss.getBlob());
var url=file.getUrl();
var html=Utilities.formatString('Right Click on this link and chose open in new tab',url);
var ui=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(ui, 'title');
}
Here's the output of this simple script

Open Page using Phantomjs (.js file)

I am new using Phantomjs, and I got the content from a website that I want. Now I want to pass the content into my website (http://test123.com). So what I want to do is, I want to open my website into a new tab in browser. However, I put the code below to test.js file and call it using phantomjs test.js but it didnt work.
var win = window.open("http://test123.com");
Anyone know how can I open a new tab in a browser using phantomjs? what should I put in my .js file ?
Try the following:
var win = window.open("http://test123.com", "_blank");

Working with automatic download file if I dont have access on the database or website

I have a task that needs to automatically download a file once the user click the button “ok”. I don't have access on the database or source code on the website, just view site access only. I don't know where and how to create it for me to start working on it. I been trying to do it using windows form but I'm not sure if its possible since the application is in the website.
How I can start doing this automatic download without any access on the applications code?
You can use the System.Net.WebClient to download a file from the web.
However you should find a pattern in the file uri.
Here is a sample:
var FileDownload = "http://addressforthefile.pdf";
var FileSaveLocation = #"C:\Downloads\myfile.pdf";
var WebClient = new System.Net.WebClient();
WebClient.DownloadFile(FileDownload, FileSaveLocation);

Print pdf file using c#, code behind and send value to javascript to generate print popup

I am working on a c# asp.net project in which, I have to print a PDF file from a directory where there are dozens of files.
I can pick the file from directory but I am not able to print that file by generating a pop up.
Does any one knows how to pass that file from back end code to java script so that It can show popup and ask for print?
P.S:I can not post code here due to PHI issue.
Thanks in advance.
open your PDF to window, get that window form Javascript,
call javascript function,
OpenPrintPopUP(){
var display_setting="toolbar=no,location=no,directories=no,menubar=no, scrollbars=no,width=650, height=850, left=100, top=25"
var prntWindow = window.open("","",display_setting);
prntWindow.document.write(printContent);
prntWindow.document.close();
prntWindow.focus();
prntWindow.print();
prntWindow.close();
}

Categories