Automating Saving a file from the web with the IE

2019-06-05 07:13发布

问题:

I'm using IE11's COM object in Powershell to authenticate to a website and pull down a list of orders. So far, my script is successfully sending the username and password as well as getting to the orders page and I can even "click" on the link that downloads the file I need. The problem is that instead of saving the file like I need to do the open/saveas dialog comes up and the script bombs.

I've attempted to have IE11 automatically save the file by editing the registry and specifying the Excel.CSV type but this didn't work. The download link is actually a button that calls a complicated Javascript function and in the end results of the CSV file I need but I've tried to decipher the JS that issues a POST request and use the Navigate() method on the IE COM object to no avail.

I realize this isn't the best way to automate a web process but I couldn't figure out how to work around all the Javascript on the page to use a cmdlet like Invoke-WebRequest. It's kinda hard to just use HTTP GETs and POSTS when EVERYTHING is Javascript!

Any help is greatly appreciated. Here's a code sample.

Function Get-PageElement ($Name,$TagName,$Id) {
    if ($Name) {
        $InternetExplorer.Document.getElementsByName($Name)
    } elseif ($TagName) {
        $InternetExplorer.Document.getElementsByTagName($TagName)
    } elseif ($Id) {
        $InternetExplorer.Document.getElementById($Id)   
    }
}


$script:InternetExplorer = New-Object -com "InternetExplorer.Application"
$script:InternetExplorer.visible = $true
$InternetExplorer.Navigate($Url)


$DownloadButton = Get-PageElement -Id btnExport
$DownloadButton.Click()

回答1:

#Start IE and navigate to your download file/location
$ie = New-Object -Com internetExplorer.Application
$ie.Navigate("http://www.your.website.here.com/awesome_stuff/DOC_1.docx")

#------------------------------
#Wait for Download Dialog box to pop up
Sleep 5
while($ie.Busy){Sleep 1} 
#------------------------------

#Hit "S" on the keyboard to hit the "Save" button on the download box
$obj = new-object -com WScript.Shell
$obj.AppActivate('Internet Explorer')
$obj.SendKeys('s')

#Hit "Enter" to save the file
$obj.SendKeys('{Enter}')

#Closes IE Downloads window
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{Enter}')


回答2:

If you can't parse the javascript and get the url you need you can use wscript to send the key strokes. Note that this is probably a terrible idea and I wouldn't recommend it if there is any way at all to parse the javascript and get the raw url to use a WebClient to download.

$wsh = new-object -comobject wscript.shell
$id = (Get-Process iexplore* | Where-Object{$_.MainWindowTitle -match "YOUR.WEB.PAGE.TITLE.HERE"}).id
if($wsh.AppActivate($id)){
    start-sleep -milliseconds 500
    $wsh.SendKeys("{S}")
    #I don't have IE11 stuck on 8 due to corporate policy you may need a second sendkeys here
    #if so insert a 500 millisecond delay
}