开发者

How can I execute a Javascript function from VBScript?

开发者 https://www.devze.com 2023-02-04 04:10 出处:网络
I\'m trying to download an Excel file from a webpage by using WSH scripts My objective is to save the Excel file from a webpage onto my machine.

I'm trying to download an Excel file from a webpage by using WSH scripts

My objective is to save the Excel file from a webpage onto my machine.

So far开发者_StackOverflow社区, the steps I've taken are: Made a vbs file, which logs into the https webpage, redirects me to another page using the second run command that opens up a new tab but after that my limited knowledge isn't able to find a solution as to how to download the file from the download link on the website to a location on my hard drive.

Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
WshShell.Run "URL", 9
wscript.sleep 3000
WshShell.SendKeys "username@"
WshShell.SendKeys "{tab}"
WshShell.SendKeys "password"
WshShell.SendKeys "{enter}"
WshShell.Run "Another_URL"

Now at this point there is a Download Link which has a javascript function javascript:download(parameters) which upon manual click generates a unique download link.

Is there any way I can download this using any Wscript? I want it to work with Windows 7 and IE 7. I have tried looking into it but it is of no avail.


I've had some success using a script similar to this

option explicit

Const URL = "http://url/to/file.xls"
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2

' request the file over http
dim http: set http = CreateObject("MSXML2.XMLHTTP")
http.open "GET", URL, false
http.send

' write the response text to a binary file
dim stream: set stream = CreateObject("ADODB.Stream")
stream.type = adTypeBinary 
stream.open
stream.write http.responseBody
stream.SaveToFile "output.xls", adSaveCreateOverWrite 
stream.close

although I haven't used it for https requests, I'm assuming that the server will accept your username and password as the 4th and 5th parameters to the MSXML2.XMLHTTP's open call.

http.open "GET", URL, false, "username@", "password"

I've tried it and it certainly works over a plain http request

see http://msdn.microsoft.com/en-us/library/ms759148(VS.85).aspx for http request and http://msdn.microsoft.com/en-us/library/ms675032(VS.85).aspx for adodb stream


Well an alternative to the above could be to use the Internet Explorer automation object. I'm not sure how you could handle a file download, but the following snippet might give you a starting point

option explicit

' create an instance of IE
dim ie: set ie = CreateObject("InternetExplorer.Application")
' load a url
ie.Navigate "http://stackoverflow.com/questions/4677595/how-can-i-execute-a-javascript-function-from-vbscript"
' sleep while IE loads the content
do while ie.busy
    WScript.Sleep 10
loop
'access the document object
dim doc: set doc = ie.document

' have IE natvigate to a link on the downloaded page. this could be your
' download link perhaps?
ie.Navigate doc.anchors(0).href 
' wait while the new page loads...
do while ie.busy
    WScript.Sleep 10
loop
' output the new content
WScript.Echo doc.documentElement.innerHTML
' close IE
ie.Quit
0

精彩评论

暂无评论...
验证码 换一张
取 消