右键单击按钮,然后将PDF文件另存为

时间:2019-11-30 19:47:13

标签: c# selenium-webdriver web-scraping web-crawler

我要访问的网页上带有PDF文件的按钮后面有一个链接。 PDF链接具有API密钥,无法直接访问。按钮的ID为company_report_link

如果这样做,reportDownloadButton.Click();的PDF在浏览器的单独选项卡中打开,但我不知道如何下载。

我尝试右键单击按钮,然后选择另存为?我可以通过此菜单打开Chrome上下文菜单,但无法选择“将链接另存为”。

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

            InputSimulator s = new InputSimulator();

            Actions action1 = new Actions(driver);
            action1.ContextClick(reportDownloadButton);
            s.Keyboard.KeyPress(VirtualKeyCode.DOWN);
            Thread.Sleep(2000);
            s.Keyboard.KeyPress(VirtualKeyCode.DOWN);
            Thread.Sleep(2000);
            s.Keyboard.KeyPress(VirtualKeyCode.DOWN);
            Thread.Sleep(2000);
            s.Keyboard.KeyPress(VirtualKeyCode.DOWN);
            Thread.Sleep(2000);
            s.Keyboard.KeyPress(VirtualKeyCode.RETURN);

我尝试使用WebClient来解决问题

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

,但由于API而无法使用。网站提供:

  

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

enter image description here

0 个答案:

没有答案