Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

c# - 使用Selenium Webdriver在无头的Chrome中下载PDF(Download PDF in Chrome headless with Selenium Webdriver)

I am trying to download PDF file from website and everything works fine in "not headless" mode.

(我正在尝试从网站下载PDF文件,并且在“非无头”模式下一切正常。)

However after enabling headless mode file is not downloaded.

(但是,启用无头模式后,不会下载文件。)

I have went through several solutions but can't figure out how to get them work.

(我已经经历了几种解决方案,但不知道如何使它们工作。)

Can anybody give me a hint?

(有人可以给我提示吗?)

Here is my code:

(这是我的代码:)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace WebDriverTest
{

    public static class WebDriverExtensions
    {
        public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
        {
            if (timeoutInSeconds > 0)
            {
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
                return wait.Until(drv => drv.FindElement(by));
            }
            return driver.FindElement(by);
        }
    }

    public class ChromeOptionsWithPrefs : ChromeOptions
    {
        public Dictionary<string, object> prefs { get; set; }
    }

    class Program
    {
        public static object downloadFilePath { get; private set; }

        static void Main(string[] args)
        {

            // declare chrome options with prefs
            var options = new ChromeOptionsWithPrefs();
            options.AddArguments("headless"); // we add headless here

            options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);  

            // Initialize the Chrome Driver // chromeOptions
            using (var driver = new ChromeDriver(options))
            {

                var sourceFile = @"C:UsersPCDownloadsFile.pdf";
                var destinationFile = @"C:empFile to.pdf";

                if (File.Exists(sourceFile))
                {
                    File.Delete(sourceFile);
                }

                // Go to the home page
                driver.Navigate().GoToUrl("https://website.com");
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
                // Get the page elements
                var userNameField = driver.FindElementById("loginForm:username");
                var userPasswordField = driver.FindElementById("loginForm:password");
                var loginButton = driver.FindElementById("loginForm:loginButton");

                // Type user name and password
                userNameField.SendKeys("username");
                userPasswordField.SendKeys("password");

                // and click the login button
                loginButton.Click();

                driver.Navigate().GoToUrl("https://website.com/878"); 

                Thread.Sleep(10000);

                driver.Navigate().GoToUrl("https://website.com/878"); // otherwise it does not work on first attempt

                // click the link to download
                var reportDownloadButton = driver.FindElementById("company_report_link");
                reportDownloadButton.Click();

                // Take a screenshot and save it into screen.png
                driver.GetScreenshot().SaveAsFile(@"screen.png", ScreenshotImageFormat.Png);

                Thread.Sleep(10000);

                File.Copy(sourceFile, destinationFile, true);



            }
        }

    }
}
  ask by Mr.V translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...