2

I hve tried every possible way available to download a PDF Report from Chrome without opening a new PDF Viewer Tab. I want to download the PDF directly on my local drive. I am Using Coypu to develop my automation framework The Code which i am using is as follows:

BrowserSession _session;

ChromeOptions options = new ChromeOptions();
String downloadFilepath = @"C:\Users\3682143\Downloads";
Dictionary<String,Object> chromeOptionsMap = new Dictionary<String, Object>();
chromeOptionsMap.Add("plugins.plugins_disabled", new String[] {"Chrome PDF Viewer"});                 
chromeOptionsMap.Add("download.default_directory", downloadFilepath);
chromeOptionsMap.Add("plugins.always_open_pdf_externally", true);
chromeOptionsMap.Add("download.prompt_for_download", false);
chromeOptionsMap.Add("pdfjs.disabled", true);
options. AddUserProfilePreference("prefs", chromeOptionsMap);
DesiredCapabilities desiredCap = DesiredCapabilities.Chrome();
desiredCap.SetCapability(CapabilityType.AcceptSslCertificates, true);
desiredCap.SetCapability(ChromeOptions.Capability, options);
_session = new BrowserSession(sessionConfig, new CustomExplorerProfileSeleniumWebDriver(desiredCap));
JSingh
  • 21
  • 1
  • 2
  • Possible duplicate of [(HTML) Download a PDF file instead of opening them in browser when clicked](https://stackoverflow.com/questions/6794255/html-download-a-pdf-file-instead-of-opening-them-in-browser-when-clicked) – Brank Victoria Nov 05 '18 at 16:21
  • Try using AutoItX found here(https://www.autoitscript.com/site/autoit/downloads/) Basically, my suggestion is to try and prompt the "Save" window of the browser (try to send keys to the browser like ctrl+s or 'something'), and after the window to save is open, use the library I've mentioned above to control the window. I think that you can use AutoItX to also navigate on the page and maybe you could download your file. Your solution might be a bit far from what I just suggested, but give AutoItX a try, and you will have one more tool on your browser to help you. – Thodoris Koskinopoulos Nov 06 '18 at 08:20

2 Answers2

0

You should be able to achieve this by setting the plugins.always_open_pdf_externally user profile preference to true.

public void DownloadPdf()
{
  var options = new ChromeOptions();
  options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);

  var chromeDriver = new ChromeDriver(options);
  var webDriver = new CustomSeleniumWebDriver(chromeDriver, Browser.Chrome);
  var session = new BrowserSession(webDriver);
  session.Visit("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
}

class CustomSeleniumWebDriver : SeleniumWebDriver
{
  private CustomSeleniumWebDriver(Browser browser) : base(browser) { }
  public CustomSeleniumWebDriver(IWebDriver webDriver, Browser browser) : base(webDriver, browser) { }
}

Tested with: Chrome 79.0.3945.117 & ChromeDriver 79.0.3945.36

mholle
  • 577
  • 1
  • 10
  • 19
0

This below code will help to save page as pdf in Selenium c#.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

protected void PDFconversion(ChromeDriver driver, string root, string rootTemp)
{
    //Grid.Rows.Add(TxtBxName.Text, TxtBxAddress.Text);
    try
    {
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        Thread.Sleep(500);
        js.ExecuteScript("setTimeout(function() { window.print(); }, 0);");
        Thread.Sleep(500);
        driver.SwitchTo().Window(driver.WindowHandles.Last());
        Thread.Sleep(500);
        string JSPath = "document.querySelector('body>print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('#destinationSettings').shadowRoot.querySelector('#destinationSelect').shadowRoot.querySelector('print-preview-settings-section:nth-child(9)>div>select>option:nth-child(3)')";
        Thread.Sleep(500);
        IWebElement PrintBtn = (IWebElement)js.ExecuteScript($"return {JSPath}");
        Thread.Sleep(500);
        PrintBtn.Click();
        string JSPath1 = "document.querySelector('body>print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('print-preview-button-strip').shadowRoot.querySelector('cr-button.action-button')";
        Thread.Sleep(1000);
        IWebElement PrintBtn1 = (IWebElement)js.ExecuteScript($"return {JSPath1}");
        PrintBtn1.Click();
        Thread.Sleep(1000);
        SendKeys.Send("{HOME}");
        SendKeys.Send(rootTemp + "\\" + "result.pdf"); // Path
        SendKeys.Send("{TAB}");
        SendKeys.Send("{TAB}");
        SendKeys.Send("{TAB}");
 
        SendKeys.Send("{ENTER}");
        Thread.Sleep(1000);       
    }
    catch (Exception ex)
    {
    }
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33