gpt4 book ai didi

c# - 从 Button 链接下载文件到 C 盘上的特定文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 20:47:48 25 4
gpt4 key购买 nike

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

链接隐藏在这后面

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

类似于: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();

编辑:

enter image description here


编辑 2:

我还不是 Stackoverflow 社区中最锋利的笔。我不明白如何使用 Selenium 来做到这一点。我已经用

完成了
        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: 'The remote server returned an error: (401) Unauthorized.'

调试器指向:

client.DownloadFile(text, fileName);

我觉得应该是真的模拟右键点击链接另存为,不然这个下载不了。另外,如果我只是点击按钮,它会在新的 Chrome 标签页中打开 PDF。


编辑 3:

应该是这样的吧?

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);

}
}
}

}
}

最佳答案

您可以尝试设置 download.default_directory 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);

如果 reportDownloadButton 是触发下载的链接,则文件应下载到您在 download.default_directory 中设置的 filePath

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

How to control the download of files with Selenium + Python bindings in Chrome

How to use chrome webdriver in selenium to download files in python?

关于c# - 从 Button 链接下载文件到 C 盘上的特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59090614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com