1

I need to download an excel file from a site. I created a WebBrowser control inside a Windows Form that navigates to the site, select a criteria, clicks a search button which opens up another page.

Now on this page there is a link button with a javascript function, that downloads an excel file when it is clicked.

the link code is the following:

<a onclick="$find('ReportViewer').exportReport('Excel');" href="javascript:void(0)" style="color: rgb(51, 102, 204); font-family: Verdana; font-size: 8pt; padding: 3px 8px 3px 32px; display: block; white-space: nowrap; text-decoration: none;">Excel</a>

I wrote a code in the WebBrowser to find the link with Text "Excel" and click it, but this opens up Internet explorer and prompts the user for download. This is an automated process, so I don't need the prompt, I need the download to happen automatically and save the file in a specific directory.

I tried to use this solution

Download a file through the WebBrowser control

But it doesn't work in my case, I think because I don't have a direct url that ends with pdf or xls. It's a javascript call.

Any ideas ?

user3547425
  • 141
  • 3
  • 13
  • Have a look WebClient.DownloadFile – akd Oct 01 '17 at 13:46
  • I tried webclient, but download file requires a URL, I don't have one. I have to simulate a click to do a postback. – user3547425 Oct 01 '17 at 18:10
  • Have a look at the library AngleSharp. That’s a really nice library. https://github.com/AngleSharp/AngleSharp/issues/563 – akd Oct 01 '17 at 18:16

1 Answers1

0
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");

foreach (HtmlElement link in links)
{
    if (link.InnerText.Equals("My Assigned")) //replace this with how you search for excel
        link.InvokeMember("Click");
}
Allanckw
  • 641
  • 1
  • 6
  • 17