首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C#中使用Selenium从Chrome下载PDF

如何在C#中使用Selenium从Chrome下载PDF
EN

Stack Overflow用户
提问于 2018-11-06 00:18:41
回答 1查看 2.6K关注 0票数 1

我已经尝试了所有可用的方法来从Chrome下载PDF报告,而无需打开新的PDF Viewer Tab。我想直接在本地驱动器上下载PDF。我正在使用Coypu开发我的自动化框架,我使用的代码如下:

代码语言:javascript
复制
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));
EN

回答 1

Stack Overflow用户

发布于 2021-06-22 23:42:41

下面的代码将有助于在Selenium c#中将页面另存为pdf。

代码语言:javascript
复制
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)
    {
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53158233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档