I've seen various solutions to saving a file using WatiN.
My current issue is similar to the others described, I need to save a PDF file, but there's a registry key that tells IE to automatically open the PDF in a new window, rather than save it.
Ideally, I could just delete that registry key and move on. However, our security policy forbids me to do that (ugh.)
I'm looking for a way to find a link, right-click, and save-as using a built-in WatiN mechanism, rather than using mouse_event in user32.dll.
Is this possible?
Thanks for the help!
This is a very old question but just as a reference for other I'll post a solution to it.
I think that the approach that you are thinking of is incorrect. If you do have a link to a PDF you do have the address of the PDF (even if you have to go through other pages to form the final download URL, which you can emulate with Watin anyway) if you do have the address of the PDF file then you should just download it:
At this point you should get the link href string (that should contain the file you want to download)
using (WebClient client = new WebClient())
{
string DownloadFolder = #"C:\DownloadFolder";
if (Directory.Exists(DownloadFolder))
{
string downloadURL = "https://www.somesite.com/somepath/filename.pdf"; // this should come from the href on the a link.
client.DownloadFile(downloadURL, DownloadFolder + "\\somefilenam.pdf");
}
}
Related
I am trying to upload a file. My application only has a Browse button and no place to send any file path. So I am unable to use SendKeys.SendWait(#"filepath"); I tried using JavaScriptExecutor but getting error while using the below code:
IWebElement upload = _driver.FindElement(By.XPath("//div[starts-with(#class,'addDoc') and contains(text(),'Browse')]"));
string filepath = #"C:/Users/../Amazon vs Walmart.pdf";
String script = "document.upload.value= " + filepath;
((IJavaScriptExecutor)_driver).ExecuteScript(script);
Runtime.evaluate threw exception: SyntaxError: Unexpected token :
Why am I getting the exception?
Interacting with file browsing windows is outside the scope of what the webdriver can do. When you click browse the browser passes you off to the OS to find the file. Javascript cannot act on these windows.
SendWait is used as a trick to pass the uri of the file to the open the window and return. If that is not working you will need to use something to interact with OS windows.
I personally use InputSimulatorCore to handle issues like this when working with IE, which has lots of these type of issues with file downloads.
Edit: If you control the application changing to a forms based upload will allow you to do the testing in Selenium and is the best option. If not you can also directly do a post to the upload endpoint.
There are multiple issues with the code you posted. First, you’re attempting to call ExecuteScript with invalid JavaScript. To wit, once your strings are concatenated, your JavaScript looks like this:
document.upload.value= C:/Users/../Amazon vs Walmart.pdf
Note that there are no quotes around the string you’re actually attempting to set. The syntactically correct JavaScript would be:
// Note carefully the quotes around the string literal.
document.upload.value='C:/Users/../Amazon vs Walmart.pdf'
To yield that, your concatenation code would need to look like this:
string script = "document.upload.value='" + filepath + "'";
The second issue is that you’re attempting to invoke uploading a file. Assuming the upload is accomplished by using standard HTML mechanisms, that means that somewhere on the page, there is am <input type="file"> element. It might be hidden, but it’s there somewhere on the page. To upload a file, you can use the SendKeys method on that element. The file upload case is one of the very few exceptions for SendKeys to the rule that elements to be interacted with must be visible to the user (at least with the most recent versions of browsers and browser drivers. If the application is using some non-standard upload mechanism, like a pure JavaScript implementation, or some sort of Flash component, then you’ll need to use some other method to communicate with that component.
Though I tried using AutoItX to upload file and it worked. Below is the code for the same -
to get AutoItX, I installed Nugget package - AutoItX.DotNet
AutoItX.ControlFocus("Open", "", "Edit1");
AutoItX.ControlSetText("Open", "", "Edit1", filepath);
AutoItX.ControlClick("Open", "", "Button1");
But still, I would like to explore using JS as well.
I have been tasked with finding a way to save files given a link to someones amazon bucket. These files are attachments uploaded to a ticketing system, and when i used their API to pull the ticket out, i am given a link like the one below(I removed all the private stuff and replaced with XXXXX)
https://s3.amazonaws.com/cdn.XXXXXXX.com/data/helpdesk/attachments/production/36012348362/original/1.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXXXXXXXFus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180808T205357Z&X-Amz-Expires=86400&X-Amz-Signature=XXXXXXXXXXXXXX-Amz-SignedHeaders=Host
As you can see, the links time out after 1 day. I am connecting 2 API's together, and i need to be able to upload an actual file into the second API instead of just placing a link that times out after a day. I have tried to use Webclient downloadfileAsync, but all i get is a blank file output. I can go to the link in my browser and do a right click, save image as, so i know its possible, but i havent had any luck programatically. Below is the code snipped i am currently using to try and download it.
using (WebClient wc = new WebClient())
{
wc.DownloadFileAsync(new System.Uri(url), "image.jpg");
}
Any insight or help would be awesome.
Thanks in advance!
I was able to get this working by not using the async version of DownloadFile. Thanks to all the people who helped me resolve this.
I'm trying to open a document on server through google docs.
I can get the path and name of the file on the server.
And then on Button click event I wrote a method to open the file through google document reader.
string path = \\xxx-yyyyy-zzz\DocShare\sample1.doc //path of the file on server
Response.Redirect("http://docs.google.com/viewer?url=" + path);
When I run this code, I'm getting something like below
Can some one help me to find where did I go wrong? Is there any other method to open any document(ppt, doc, pdf etc) using google document reader.
TIA
What is \\xxx-yyyyy-zzz\DocShare\sample1.doc and where it is ?
A relative url dosen't specify a protocol & domain which makes the
browser to assume the document is referd from the same site/domain.
Please verify if you can access the document itself with the relative path.
http://docs.google.com/viewer?url=https://s3.amazonaws.com/scotchio/First-Node-App.pdf works for me as i can access the pdf. Try using the absolute url of the document
It doesn't work most likely because the file is not accessible from the Internet. The google doc previewer needs to be able to access the file in order to display it.
Requirement:
I have a pdf file in my machine. Using console\windows application, i need to open that pdf file from browser itself. I am getting answers to open pdf file in browser using ASP.NET. But i need answer to open pdf in browser using console app.
I tried the below code:
string localURL = #"C:\MyLocation\apllication demo.pdf";
System.Windows.Controls.WebBrowser webbrowser = new System.Windows.Controls.WebBrowser();
webbrowser.Navigate(localURL);
But no use. It asusual opening in its default application.
This is simply because you are supplying a wrong path.
For browser, the path would look like this:
string localURL = "file:///C:/MyLocation/apllication%20demo.pdf"
Note that %20 is a space character,so perhaps use string.Replace(" ","%20") when building the Url.
Is there any way in C# to open a browser using a link and save the loaded html page ?? Actually i don't want to inform server that i am using any software or script .
Actually I want data from this link : http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&rt=nc&item=120840650200&si=a8iGAIchyvEbn7KveYFZ5QbEE7o%3D&print=all&category=10363
if I try to download the page using webclient it send me another page not the original one.
when i load the previous url in a browser it is redirected to this link:
http://www.ebay.com/itm/ws/eBayISAPI.dll?ViewItem&rt=nc&item=120840650200&si=a8iGAIchyvEbn7KveYFZ5QbEE7o%3D&print=all&category=10363 which is the original page I want to download
so I just wanted to open the browser using the url and save the loaded page.
Thanks in advance
Do you really need to open the browser, or is just (inconspicuously) retrieving the file enough? If so, you can use System.Net.Webclient or System.Net.HttpWebRequest/HttpWebResponse. The former option is much easier, but the latter will allow you to set your own user-agent string to one that would match that of common web browsers.
Straight from MSDN
WebClient client = new WebClient();
Byte[] pageData = client.DownloadData("http://www.contoso.com");
string pageHtml = Encoding.ASCII.GetString(pageData);
Console.WriteLine(pageHtml);