我已经尝试了所有可能的方法来从Chrome下载PDF报告,而无需打开新的PDF Viewer标签。我想直接在本地驱动器上下载PDF。我正在使用Coypu开发自动化框架 我正在使用的代码如下:
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));
答案 0 :(得分:0)
您应该可以通过将plugins.always_open_pdf_externally
用户个人资料偏好设置设置为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) { }
}
经过测试:Chrome 79.0.3945.117和ChromeDriver 79.0.3945.36
答案 1 :(得分:0)
以下代码将有助于在 Selenium c# 中将页面另存为 pdf。
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)
{
}
}