saving file with download prompt with vbscript - vbscript

I'm using VBscript to use IE to open a webpage - login - and save some data to a file.
Why use IE?
- I can use VBScript to interact with the webpage
- The webpage uses ajax to do verification and signing in, so simply using MSXML2.XMLHTTP wont work.
After IE log into the site, I direct it to the page I want to save as a file, the problem is this page is returning JSON data (filename.json) - so IE is prompting to open/save/saveas rather than just opening the page, so I cant save the data of the page.
The code is below. Any help is much appreciated.
Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
IE.Visible = True
IE.Navigate "https://www.somesite.com"
Wait IE
With IE.Document
.getElementByID("username").value = "myusername"
.getElementByID("password").value = "mysecret"
.getElementsByName("Login")(0).Click
End With
Wait IE
'# all fine here, logged in and redirected to members page
IE.Navigate "https://www.somesite.com/api/data"
' # this link returns json data
'# this is where everything stops because Im getting a download/save as prompt in IE
EDIT: ok, I was able to get IE to display the content rather than downloading it by editing the registry to treat .json file as text files,Now the problem is the page is a json file, not a HTML file, so I cant get it's content by calling IE.Document.Body.InnerHTML like normal HTML pages.
How would I get the content of the page and assign it to a variable so I can process it?

oh, I just put this in my code:
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite "HKEY_CLASSES_ROOT\.json\Content Type", "text/plain", "REG_SZ
That way the file is treated as a textfile within IE and it will just open in the page. Then I can use the normal objIE.Document.Body.InnerText to get the json contents. Then I pass that variable onto my function that decodes it all.
Hope this helps someone, because all the results on google I found did not help me. Only reason I posting on this old thread!
Cheers,
Ross

Related

How to convert IHTMLImgElement to image

I am automating Internet Explorer using SHDocVW.dll and MSHTML with C#, and I wish to save an image from the page to the disk (JPEG format).
I can't use the WebClient class to download the image; if I do it, I end up downloading the site's login page. I can't print the screen either, because the browser has to remain invisible during this process, running in the background.
I have tried to do the following:
IHTMLImgElement imgElement = ...;
IHTMLControlRange imgRange = ...;
imgRange.add(imgElement as IHTMLControlElement);
imgRange.execCommand( "copy", false, null );
This does nothing. I am not able to extract anything from the clipboard. Every solution I found didn't work for me.
Your webclient approach is probably missing cookies... see How do I log into a site with WebClient? for an example that handles cookies.
your code looks fine except the user has to change the security setting to enable clipboard access. If the image is cached on disk you can dig the WinInet cache after parsing the page for the image location.

Watir - Cannot get through basic authentication on IE8

I'm having trouble getting through basic authentication on one of our websites on IE8 using Watir.
When I navigate(goto) the url, the basic authentication dialog is displayed showing 'Connect to ' as the title, with user name, password edit fields and OK, Cancel buttons (the usual!).
In Watir, after passing the following code:
b = Watir::Browser.new
b.goto '<our url>'
the dialog is displayed but the goto statement never completes. If I cancel or login manually, then the goto statement returns a number (this is in IRB by the way).
I've done lots of investigation and tried out all sorts of ideas but now seem to be stumped!
When we login to the site manually, we have to pass our credentials like below:
User Name: <domain>\<username>
Password: <password>
I've tried this in the url string i.e.
http://<domain>\<username>:<password>#<url>:<port>
http://lnd\mbartram:mypassword#xxx.xx.xx.xx:xxxx
but this doesn't work in code or if I enter it manually in the address bar.
Perhaps I need to escape the \ part to get this to work but how?
Any help is greatly appreciated.
M.
Ok, using a bit of a hack but it works consistently for me. I'm using a simple Wscript.Shell SendKeys method. See below:
`start #{theURL]`
loginDialog = WIN32OLE.new('Wscript.Shell')
loginDialog.AppActivate('Connect to #{ip}')
sendKeys = "#{userName}{TAB}"
loginDialog.SendKeys(sendKeys)
sleep(1)
sendKeys = "#{password}{TAB}{ENTER}"
loginDialog.SendKeys(sendKeys)

how to close IE9 browser session that has a popup message "Do you want to open or save file" displayed

on my web application, after clicking a download button, a popup message with content "Do you want to open or save "abc.txt" from this site?" with 3 buttons ("Open", "Save" & "Cancel") will be displayed at the bottom of page.
I'm trying to close/quit this browser session with below codes:
#driver.execute_script "window.onbeforeunload = function(e){};"
#driver.quit
However, the browser is NOT (but should be) closed. I'm working with Selenium Ruby Webdriver. Please guide me a way to resolve this problem. Thanks so much.
Note that with the above codes, I'm able to close IE9 browser that has the popup message "This page is asking you to confirm that you want to leave - data you have entered many not be saved" with "Leave Page" and "Stay on Page" buttons successfully. But, codes do NOT work in case the popup message with content "Do you want to open or save "abc.txt" from this site?" with 3 buttons ("Open", "Save" & "Cancel") displayed.
WebDriver has no control over these types of "Save file" dialog prompts.
Please peruse this article on this subject (note, his examples are in Java, but they can all be ported easily and with less code in Ruby). http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/
Thus, I would recommend not even clicking the link in question and trying to deal with the dialog. Instead, grab the href value and initiate a Net::HTTP request to it like this SO response shows you how to do: https://stackoverflow.com/a/4581116/1221475 . You can then check the file for correct contents and such using standard Ruby and file parsers.
This is a hack by Dave Haefner. It is written for Java/Selenium combination, but you can easily convert it to Ruby syntax.
If you don't care if a file was downloaded or not and you want to confirm only that a file can be downloaded, you can use an HTTP request. Instead of downloading the file you'll receive the header information for the file which contains things like the content type and length. With this information, you can confirm the file is you expect.
String link = driver.findElement(By.cssSelector("download-link-element")).getAttribute("href");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpHead request = new HttpHead(link);
HttpResponse response = httpClient.execute(request);
String contentType = response.getFirstHeader("Content-Type").getValue();
int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
assertThat(contentType, is("application/octet-stream"));
assertThat(contentLength, is(not(0)));

Unexplained Inconsistency when Downloading an XLS file with Ruby Mechanize after redirect

I have a script that visits fcc.gov, then clicks a link which triggers a download:
require "mechanize"
docket_number = "12-268" #"96-128"
url = "http://apps.fcc.gov/ecfs/comment_search/execute?proceeding=#{docket_number}"
agent = Mechanize.new
agent.pluggable_parser.default = Mechanize::DirectorySaver.save_to 'downloads'
agent.get(url) do |page|
link = page.link_with(:text => "Export to Excel file")
xls = agent.click(link)
end
This works fine when docket_number is "12-268". But when you change it to "96-128", Mechanize downloads the html of the page instead of the desired spreadsheet.
The urls for both pages are:
http://apps.fcc.gov/ecfs/comment_search/execute?proceeding=12-268 (works)
http://apps.fcc.gov/ecfs/comment_search/execute?proceeding=96-128 (this is where I need help)
As you can see, if you visit each page in a browser (I'm using Chrome) and click "Export to Excel file", a spreadsheet file is downloaded and there is not problem. "96-128" has many more rows, so when you click on the Export link, it takes you to a new page that refreshes every 10 seconds or so until the file begins downloading. How can I get around this and why is there this inconsistency?
Clicking Export on 96-128 takes you to a page that refreshes using this kind of a tag (I've never heard of it before):
<meta http-equiv="refresh" content="5;url=/ecfs/comment_search/export?exportType=xls"/>
By default, Mechanize will not follow these refreshes. To get around that, change a setting on agent:
agent.follow_meta_refresh = true
Source: https://stackoverflow.com/a/2166480/94154
The proceeding 12-268 has 48 entries, 96-128 has 4046.
When I click at 'Export to Excel File' on the latter, there sometimes is a page saying:
Finished processing 933 of 4046 records.
Click if this page does not reload automatically.
I guess mechanize is seeing this, too.

Wicket - Internet Explorer double submit

I have a big problem with Internet Explorer 7 and 8.
SITUATION:
I have a FORM that build a Medical Prescription. When I hit the save button, the script saves the DomainObject on DB and set a boolean property (of panel where the form is added) called "saved" to true and a byte[] property called PDF with bytestream.
On RenderHead of Panel, I read this boolean and, if is true, I force the trigger of a hidden button with this code:
String js = "$('#" + printPDF.getMarkupId() + "').click();";
response.renderOnDomReadyJavaScript(js);
The button executes this code:
ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(new ByteArrayResourceStream(pdf, "application/pdf"));
handler.setFileName("foo.pdf");
RequestCycle.get().scheduleRequestHandlerAfterCurrent(handler);
This code work perfecly on FF and Chrome. The Browser download windows appears and the user can save the PDF on HD.
Unfortunally, Internet Explorer has that damn security behavior that is triggered when a site require something to download. That warning require a user validation. A yellow Bar appear and the user is force to hit "Download".
screenshot http://imageshack.us/a/img198/1438/securityg.jpg
When I hit Download File, the form is submitted again with the exact state I had when I hit save the first time. So no previous INSERT on DB is already committed; The Session is resetted to the previous state etc...
The result is a double INSERT on DB of the Domain Ojbect.
Any clue to resolve this?
The problem is that you click download link programaticly instead to redirect browser to an URL or open an URL by JS window.open(url). Click a link looks like an unwanted operation that is sometimes restricted by browser.

Resources