Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automatically click on "do you want to open or save?"

Tags:

c#

selenium

I am wondering if there is a c# or selenium solution to the following:

enter image description here

I am using selenium to download a file off a webserver.

unfortunately in IE9 there is no way to disable this popup screen.

is there a solution in c# for clicking on the SAVE button?

like image 582
Alex Gordon Avatar asked Jul 23 '12 22:07

Alex Gordon


1 Answers

Client

WebClient client = new WebClient();
byte[] file = client.DownloadData("http://domain.com/default.aspx");
File.WriteAllBytes("helloworld.txt", file);

Server

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (MemoryStream memory = new MemoryStream())
        using (StreamWriter writer = new StreamWriter(memory))
        {
            // The bytes written can be anything, does not have to be text
            writer.WriteLine("hello, world");
            writer.Flush();
            Response.BinaryWrite(memory.ToArray());
        }

        Response.AddHeader("Content-Disposition", "Attachment; filename=helloworld.txt");
        Response.AddHeader("Content-Type", "application/octet-stream");
        Response.End();
    }
}

As you can see with the above example, not only do I not know the physical location of the file, but there is no physical location! It is something I just wrote to memory and then wrote the raw bytes to the response, but WebClient.DownloadData() will still be able to download the bytes all the same. It doesn't care where the bytes come from. Hope this helps.


Additional Information:

Some further background information further explaining why the above works.

A browser's primary job is to send HTTP requests and handle the response. Fortunately, they handle a lot of the grunt work for us. Showing a simple web page involves sending an HTTP-GET to the server and receiving bytes back in the response body, decoding those bytes into text, and parsing that text to render an HTML web page. It knows to handle the response like that because it has a Content-Type header of Text/HTML. While this is what it does most of the time, browsers can also handle other MIME types, if the Content-Type is text/plain it will decode the bytes and just display them without trying to parse it. Text/XML will usually allow you to collapse and expand XML nodes, etc. Again this is all dependent on how the browser is programmed to handle that particle MIME type.

When you get a Save As dialog box in a browser, that is simply the browser's way of handling a response with a Content-Disposition: Attachment header. This header tells the browser not to try to render or display the content, but to download it as an attachment instead.

When you use the WebClient / HttpWebRequest classes, you are essentially writing your own miniature browser, however the implementation of how MIME types / HTTP headers are handled is entirely up to you. And this can allow you to save the bytes from a Content-Disposition response (or any response for that matter) without prompting with a Open or Save dialog box.

like image 145
Despertar Avatar answered Nov 08 '22 17:11

Despertar