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 --
Related
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
I want to download an image from a cartoon website. and my app is WinForm,not WebForm.
So let's say that there is an image on the a.html.
Normally, when I click the previous page and am redirected to this page,
there will be a image :"image is loading",let's say A.jpg, in the same block.
After 5 seconds, the real one,let's say B.jpg, will be displayed.
So what I got is only the caching image rather than the one,B.jpg, which I want.
So..... how should I do it?
Thanks in advance.
ps: I have posted this qustion for more than 48 hours, and only got a few of answers which don't solve my problem.
I am wondering that why there are only 2 people posted their answers?
Is my question not clear?
If any, please let me know.
Thanks
EDIT: Original answer removed since I misunderstood the question entirely.
What you want to do is basically HTML scraping: using the actual html of the page to discover where files are hosted and download them. Because I'm not sure if there are any legal reasons that would prevent you from downloading the image files in this manner, I'm just going to outline an approach to doing this and not provide any working samples or anything. In other words, use this information at your own risk.
Using fiddler2 in Firefox, you should be able to find the domain and full url that one of the images is downloaded from. Basically just start fiddler2, navigate to the site in firefox, and then look for the biggest file that is downloaded. That will tell you exactly where the image is coming from.
Next, take a look at the HTML source code for the page you are viewing. The way this particular site works, it looks like it hides the previous/next downloads in a swf or something, but you can find the urls in the javascript for the page. Look for a javascript array called picArr.
To download these using a WinForms app, I would use the WebRequest object. Create a request for each image url and save the response to disk.
I want firefox to open C:\somefile.txt on the local drive. I've tried response.redirect with the "file:///" prefix, but it just inserts "Object moved to here." into the page. I also want to htmlencode the path because it could contain special chars...
Edit: To be clear: C:\somefile.txt is a file on the client box running the browser
You can't. Web pages are explicitly not allowed to link or redirect to local content. It's a security measure.
To be honest I don't quite know why Response.Redirect("file:///aaa") isn't working for you. Without reading the RFC, or testing it myself, I don't know.
-- Edit:
Tested myself; definitely doesn't work.
Failing someone showing you how, you can just write write a link to it, or use some (admittedly lame) javascript (to be written to the client, instead of Redirecting) like so:
<script language="javascript">document.location = "file:///aaaa";</script>
I've been struggling to find an exmample of some C# code (I'm using C# Visual Studio 2008 Express) that can programmatically save an entire web page (given a URL) including the images and formatting (e.g. CSS). The intention is that in a subsequent phase I'd ship this off (not sure how yet) so it could be viewed later via a browser.
Is there an example of the most simple approach (leveraging the .NET Framework methods) to save an entire web page? Saving as one page with a subdirectory for images, or otherwise. Basically the same as what you get with browsers when you say "save entire web page".
The simplest way is probably to add a WebBrowser Control to your application and point it at the page you want to save using the Navigate() method.
Then, when the document has loaded, call the ShowSaveAsDialog method. The user can then save the page as a single file, or a file with images in a subdirectory.
[Update]
Having now noticed "programatically" in your question, the above approach is not ideal as it requires either user involvement or delving into the Windows API to send input using SendKeys or similar.
There is nothing built-in to the .NET Framework that does all of what you ask.
So my approach revised would be:
Use System.NET.HttpWebRequest to get the main HTML document as a string or stream (easy).
Load this into a HTMLAgilityPack document where you can now easily query the document to get lists of all image elements, stylesheet links, etc.
Then make a separate web request for each of these files and save them to a subdirectory.
Finally update all relevent links in the main page to point to the items in the subdirectory.
In effect you would be implementing a very simple web browser. You may run into issues with pages that use JavaScript to dynamically alter or request page content, but for most pages this should give acceptable results.
From code Project: ZetaWebSpider
It's definitely not elegant, but you could navigate a System.Windows.Forms.WebBrowser to the URL and then call its ShowSaveAsDiagog() method to save the page.
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.