How to upload a file using WebClient if served by javascript

I am writing an automated test solution based on Selenium WebDriver. I need a browser independent method that can provide file uploads.

My method, which relies on the System.Net.WebClient class, can successfully download files from the site if there is an attribute (like href or src ). from which to specify the location of the file.

However, I am having problems when an element that normally contains a link does not have a file pointer. Instead, this element has a CSS class bound to a Javascript click event. When the element is clicked, Javascript fires the window.open action on the dynamically generated link.

Any ideas on how I can successfully grab this file link from my C # code?

+3


source to share


1 answer


If I understand you correctly, you are creating a client application and the behavior of your server is out of control. You want to emulate the following behavior in a real browser:

  • The client navigates to the url.
  • The client clicks on a link (or other element) that triggers a JavaScript function.
  • The JavaScript function redirects the client to the correct URL to download the asset.
  • The client downloads the asset.

You cannot do this with a WebClient class that does not expose a click or execute JavaScript in any way. The WebClient instance is not a browser. It is simply a class that provides a way to send data to and receive data from a given URL.



This does not directly answer your question, but there are arguments that when using an automation tool you should stick with what you can do with an IE tool, you should use a browser to do what you need to do. If for some reason you really need to load the result of this click event, but cannot do so via selenium, then you will probably need to use a mute browser. This question discusses some of the options you could use.

Several more options are discussed in this question.

-2


source







All Articles