C# 将文件从按钮链接下载到C驱动器上的特定文件夹

C# 将文件从按钮链接下载到C驱动器上的特定文件夹,c#,selenium,selenium-webdriver,web-scraping,C#,Selenium,Selenium Webdriver,Web Scraping,我正在抓取网页并导航到正确的位置,但是作为整个c#世界的新手,我一直在下载pdf文件 林克躲在这后面 var reportDownloadButton=driver.FindElementById(“公司报告链接”) 它类似于:www.link.com/key/489498-654gjgh6-6g5h4jh/link.pdf 如何将文件下载到C:\temp\ 这是我的密码: using System.Linq; using OpenQA.Selenium.Chrome; namespace W

我正在抓取网页并导航到正确的位置,但是作为整个c#世界的新手,我一直在下载pdf文件

林克躲在这后面

var reportDownloadButton=driver.FindElementById(“公司报告链接”)

它类似于:
www.link.com/key/489498-654gjgh6-6g5h4jh/link.pdf

如何将文件下载到C:\temp\

这是我的密码:

using System.Linq;
using OpenQA.Selenium.Chrome;

namespace WebDriverTest
{
    class Program
    {
        static void Main(string[] args)
        {

            var chromeOptions = new ChromeOptions();
            chromeOptions.AddArguments("headless");

            // Initialize the Chrome Driver // chromeOptions
            using (var driver = new ChromeDriver(chromeOptions))
            {
                // Go to the home page
                driver.Navigate().GoToUrl("www.link.com");
                driver.Manage().Timeouts().ImplicitWait = System.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("www.link2.com");
                driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);

                var reportSearchField = driver.FindElementByClassName("form-control");

                reportSearchField.SendKeys("Company");

                var reportSearchButton = driver.FindElementById("search_filter_button");
                reportSearchButton.Click();

                var reportDownloadButton = driver.FindElementById("company_report_link");
                reportDownloadButton.Click();

编辑:


编辑2:

我还不是这个社区最聪明的人。我不知道如何使用硒。我已经做了

        var reportDownloadButton = driver.FindElementById("company_report_link");
        var text = reportDownloadButton.GetAttribute("href");
        // driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);

        WebClient client = new WebClient();
        // Save the file to desktop for debugging
        var desktop = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
        string fileName = desktop + "\\myfile.pdf";
        client.DownloadFile(text, fileName);
然而,网页似乎有点棘手。我越来越

System.Net.WebException:'远程服务器返回错误:(401) 未经授权。”

调试器指向:

client.DownloadFile(text, fileName);
我认为它应该真的模拟右键点击和保存链接为,否则这个下载将无法工作。另外,若我只需点击按钮,它会在新的Chrome选项卡中打开PDF


EDIT3:

应该是这样吗

using System.Linq;
using OpenQA.Selenium.Chrome;

namespace WebDriverTest
{
    class Program
    {
        static void Main(string[] args)
        {

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

    // declare prefs
        options.prefs = new Dictionary<string, object>
        {
            { "download.default_directory", downloadFilePath }
        };

    // declare driver with these options
    //driver = new ChromeDriver(options); we don't need this because we already declare driver below.

            // Initialize the Chrome Driver // chromeOptions
            using (var driver = new ChromeDriver(options))
            {
                // Go to the home page
                driver.Navigate().GoToUrl("www.link.com");
                driver.Manage().Timeouts().ImplicitWait = System.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("www.link.com");
                driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);

                var reportSearchField = driver.FindElementByClassName("form-control");

                reportSearchField.SendKeys("company");

                var reportSearchButton = driver.FindElementById("search_filter_button");
                reportSearchButton.Click();

                driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
                driver.Navigate().GoToUrl("www.link.com");

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

                // if clicking does not work, get href attribute and call GoToUrl() -- this may trigger download
                var href = reportDownloadButton.GetAttribute("href");
                driver.Navigate().GoToUrl(href);

                }
            }
        }

    }
}
使用System.Linq;
使用OpenQA.Selenium.Chrome;
命名空间WebDriverTest
{
班级计划
{
静态void Main(字符串[]参数)
{
//使用prefs声明chrome选项
var options=new ChromeOptionsWithPrefs();
options.AddArguments(“headless”);//我们在这里添加headless
//声明优先权
options.prefs=新字典
{
{“download.default_目录”,downloadFilePath}
};
//使用这些选项声明驱动程序
//driver=新的ChromeDriver(选项);我们不需要它,因为我们已经在下面声明了驱动程序。
//初始化Chrome驱动程序//chromeOptions
使用(var驱动程序=新的色度驱动程序(选项))
{
//转到主页
driver.Navigate().gotour(“www.link.com”);
driver.Manage().Timeouts().ImplicitWait=System.TimeSpan.FromSeconds(15);
//获取页面元素
var userNameField=driver.FindElementById(“loginForm:username”);
var userPasswordField=driver.FindElementById(“loginForm:password”);
var loginButton=driver.FindElementById(“loginForm:loginButton”);
//键入用户名和密码
userNameField.SendKeys(“用户名”);
userPasswordField.SendKeys(“密码”);
//然后单击登录按钮
loginButton.Click();
driver.Navigate().gotour(“www.link.com”);
driver.Manage().Timeouts().ImplicitWait=System.TimeSpan.FromSeconds(15);
var reportSearchField=driver.FindElementByClassName(“表单控件”);
reportSearchField.SendKeys(“公司”);
var reportSearchButton=driver.FindElementById(“搜索过滤器按钮”);
reportSearchButton。单击();
driver.Manage().Timeouts().ImplicitWait=System.TimeSpan.FromSeconds(15);
driver.Navigate().gotour(“www.link.com”);
//点击链接下载
var reportDownloadButton=driver.FindElementById(“公司报告链接”);
reportDownloadButton。单击();
//如果单击不起作用,请获取href属性并调用gotour()--这可能会触发下载
var href=reportDownloadButton.GetAttribute(“href”);
driver.Navigate().gotour(href);
}
}
}
}
}

您可以使用它。

您可以使用它。

您可以尝试设置
下载。默认目录
Chrome驱动程序首选项:

// declare chrome options with prefs
var options = new ChromeOptionsWithPrefs();

// declare prefs
    options.prefs = new Dictionary<string, object>
    {
        { "download.default_directory", downloadFilePath }
    };

// declare driver with these options
driver = new ChromeDriver(options);


// ... run your code here ...

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

// if clicking does not work, get href attribute and call GoToUrl() -- this may trigger download
var href = reportDownloadButton.GetAttribute("href");
driver.Navigate().GoToUrl(href);
//用prefs声明chrome选项
var options=new ChromeOptionsWithPrefs();
//声明优先权
options.prefs=新字典
{
{“download.default_目录”,downloadFilePath}
};
//使用这些选项声明驱动程序
驱动程序=新的色度驱动程序(可选);
// ... 在这里运行您的代码。。。
//点击链接下载
var reportDownloadButton=driver.FindElementById(“公司报告链接”);
reportDownloadButton。单击();
//如果单击不起作用,请获取href属性并调用gotour()--这可能会触发下载
var href=reportDownloadButton.GetAttribute(“href”);
driver.Navigate().gotour(href);
如果
reportDownloadButton
是触发下载的链接,则文件应下载到您在
download.default\u目录中设置的
filePath

这两个线程都不在C#中,但它们谈到了类似的问题:


您可以尝试设置
下载。默认目录
Chrome驱动程序首选项:

// declare chrome options with prefs
var options = new ChromeOptionsWithPrefs();

// declare prefs
    options.prefs = new Dictionary<string, object>
    {
        { "download.default_directory", downloadFilePath }
    };

// declare driver with these options
driver = new ChromeDriver(options);


// ... run your code here ...

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

// if clicking does not work, get href attribute and call GoToUrl() -- this may trigger download
var href = reportDownloadButton.GetAttribute("href");
driver.Navigate().GoToUrl(href);
//用prefs声明chrome选项
var options=new ChromeOptionsWithPrefs();
//声明优先权
options.prefs=新字典
{
{“download.default_目录”,downloadFilePath}
};
//使用这些选项声明驱动程序
驱动程序=新的色度驱动程序(可选);
// ... 在这里运行您的代码。。。
//点击链接下载
var reportDownloadButton=driver.FindElementById(“公司报告链接”);
reportDownloadButton。单击();
//如果单击不起作用,请获取href属性并调用gotour()--这可能会触发下载
var href=reportDownloadButton.GetAttribute(“href”);
driver.Navigate().gotour(href);
如果
reportDownloadButton
是触发下载的链接,则文件应下载到您在
download.default\u目录中设置的
filePath

这两个线程都不在C#中,但它们谈到了类似的问题:


如何从中获取链接作为字符串<代码>var reportDownloadButton=driver.FindElementById(“公司报告链接”)我不知道“公司报告链接”是什么,但通常您可以访问属性或innertext