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));
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
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)
{
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.