How to catch download link generated after buttonclick event - c#

So as for input:
I am using C# and Selenium Webdriver to automate some verification on a website. Browser is IE9.
The steps that I am working on:
I have a table that was generated by ajax query. When I click print button it returns me a file to download that can be printed.
The issue that I need to catch the link to the file that is proposed for downloading and I have run out of ideas how to do that.
So I would be grateful to hear any advice of skilled users =).
Updated 08/01/14:
Ups sorry, I forgot to say that there is no link, actually button click triggers either a JS or ajax request that creates a document and then only then the link is generated and Open/Save IE dialog is displayed.
Updated
Link HTML
<a id="ucRadGrid_lnkPrintPDF" onclick="ucRadGrid.print();" href="javascript:__doPostBack('ucRadGrid','PDF')">

What i do, is i never allow my webdriver to manage a download. What i'll do is use pure C# to download the file for me.
You can do this simply by finding the link's href attr, and downloading that. Here is some pseudo-code:
var href = driver.FindElement(By.ID("download_link").GetAttribute("href");'
DownloadFile(href, "my file.ext");
From there, you can do what you need to. Validate the text using pure C#, etc.
EDIT After your comment below:
What you can to do is find the URL of the resource you want to download. That might require even playing around with the JS in the page. Find the function that downloads the file, and either execute that code, or parse through the function for the URL, and then execute DownloadFile

Related

AxWebBrowser : Download file when already logged in

I am trying to "download" a file using the AxWebbrowser in VB.Net language (though, the answer could be in C#, I don't matter). The browser is already logged in, so I tried to catch the download, but for now the test is done with a PDF and I think it is opened directly in the browser. OK, lets put you in the context.
AxWebbrowser going to a login page which I use JavaScript to fill it and continue
Navigating to a message page which includes links to attached files
Trying to download these files (in fact, trying to get their bytes to transform them in Base64 and include them in my HTML returned... so now the only problem is getting the bytes, after that, conversion + inclusion is something I know)
So, I tried to pass to the browser the URL directly and detect the download and catch the bytes ==> Not able to.
I tried to use a WebClient which I set the cookies, but it doesn't work. Though, using this and comparing with Chrome, I see the cookies aren't the same (in fact I can highly presume there is one important missing).
So, either why I don't get all the cookies or how could I get the bytes from these files?
I got it!!! Finally thought to search how to "save as" a file and then got it.
https://www.codeproject.com/tips/659004/download-of-file-with-open-save-dialog-box
Using GetGlobalCookies got me the proper cookies and the file is downloaded correctly using Webclient. Oh yeah!
-- I removed my duplicate answer to another question. I answered another question by mistake --

Upload while switching pages (C# jQuery)

i'm searching for a way to upload huge files with jQuery and C#/VB.net (ASP).
I want to open an uploadform, select a file and upload it. After firing the upload (submit) it must be possible to leave the page with the upload form, and switching to another page within the same domain (Fire & Forget).
Can this be realized with jQuery and C# / VB.Net. On which Keywords i have to look to find a solution to get this working or is this impossible ?
Greetings
Marcus
You can use an async upload like following and then write a code to redirect a page.
http://www.dotnetjalps.com/2011/12/async-file-upload-with-jquery-and.html
at the end of process request method write following code.
context.Response.Redirect("YourPage.aspx");

File upload that looks like ajax

I am trying to create a form which allow async file uploading with asp.net. I realize you cannot upload a file with ajax per se so I am examining alternatives
What is the best way to do this? Create an Iframe on the page with the entire form including the file input? Can I on the parent to the frame have the submit button which forces the frame to submit and then displays some sort of spinner to indicate file is uploading? Ideally upon completion I'd like to redirect the user to another page. Is there a somewhat easy way to do this???
Have you tried using one of the jquery plugins vice doing it by hand?
http://aquantum-demo.appspot.com/file-upload
Why not use the ASP.NET AJAX Control Toolkit's AsyncFileUpload control? It's free and works pretty well.
You could use http://jquery.malsup.com/form/#file-upload
Have it post to a page that will handle a file upload on the server side in your usual way.
I like to have the page return JSON with a success/failure flag and message, then parse the response to determine if the upload succeeded.

C# WebClient() Downloading a specific part of a website

Ok I want to develop a scraping application to download specific text inside a div tag on a website. Lets take for example
<div class="main_content">WOTEVER GOES IN HERE, GOES IN HERE</div>
How would I go about downloading the text
WOTEVER GOES IN HERE, GOES IN HERE
I understand I would need to use WebClient() with
.DownloadFile(sourceFileAddress, destinationFilePath);
Thankyou
HTTP requests are done on the "resource" basis and this resource is a file -> you can't download some text from a page, you need to download the file and parse it.
If the file is eg. very big and you know the div is at the beginning you may consider using TCP/IP sockets and handling the request and response manually (parsing on the fly), but I don't really know if that would give you any benefit.

Read & display text file in javascript

The user needs to click on browse button to browse his system .He then selects a text file & clicks ok.Once he clicks ok all the data in the text file should be displayed in a text area.How do I do that? I am using JavaScript & c# designing aspx pages.It would be preferable if i avoid round trip to the server.
You can't do it without a trip to the server, the only way for you to get the content of the file is by submitting it as part of a form. You can make the trip to the server happen in an iframe via XHR and then update the text area with the result from the XHR call, so it sort of seems like one wasn't involved, but you can't directly access the content of files of the user's machine, for obvious reasons.
I know you said you would prefer a round trip, but its the only way you are going to be able to accomplish what you want.
You could put the file upload in an iframe, and do the upload behind the scenes (No page refresh, gmail does this :) ) then use AJAX to download the data and insert it into the textarea.
It can't in general be done, as answers here outline.
However, it can be done in Firefox 3+ only, using the uploadfield.files array. Other browsers would have to fall back to the server round-trip.
For security reasons, JavaScript cannot access the local filesystem like that.
Javascript cannot do that without putting a severe security risk on the user. That said, the file will need to be posted to your server.
As other posters here have indicated, you're not allowed to access the local filesystem from Javascript directly. But you can set up an action on your server to take the file form POST input, and simply echo the data right back out to the response. If you hide an iframe inside your page as the form POST target, that response data can appear in the hidden iframe, and then the page won't have to reload. Then once the iframe has loaded with the text, you can use JS to pull the text out of the iframe, and put it into the text area that you're interested in.
Alternately, if you're inclined to restrict usage to Firefox users with an extension, you should be able to accomplish this without a roundtrip using a Greasemonkey user script (see www.greasespot.com) or something like it, that uses the custom Mozilla extensions.

Categories

Resources