2

我创建了一个使用 Internet explore 11 登录到 https 网站的小脚本。我手动单击鼠标将文件下载到 IE,但我收到保存打开文件的提示我希望能够绕过这个因为我打算在后台运行这个?我是否在 ie.navigate(url) 末尾缺少一个参数来保存文件?正如我上面所说,我为此使用 IE11,降级不是一种选择。

任何帮助都会很棒

$username= get-content -Path c:\username.txt
$password= get-content -Path c:\password.txt

$ie = New-Object -ComObject internetExplorer.Application
$ie.Visible= $false
$ie.fullscreen = $false

$ie.Navigate("https://www.mft1.firstdataclients.com/cgi-bin/messageway/mwi.cgi")

while ($ie.Busy -eq $true){Start-Sleep -seconds 2}  

$usernamefield = $ie.Document.getElementByID('user')
$usernamefield.value = $username

$passwordfield = $ie.Document.getElementByID('password')
$passwordfield.value = $password

$ie.document.getElementById("request").click()

while ($ie.Busy -eq $true){Start-Sleep -seconds 2}

$ie.Navigate("https://www.mft1.firstdataclients.com/cgi-bin/messageway/mwi.cgi?    request=ViewDownloaded")

while ($ie.Busy -eq $true){Start-Sleep -seconds 2}


$ie.Document.getElementsByTagName('a')| where-object {$_.href -match "request=TextDownload"} | where-object {$_.href -match 'CL9DFMDE'}| select -First 1 -ExpandProperty 'href'

$link=$ie.Document.getElementsByTagName('a')| where-object {$_.href -match "request=TextDownload"} | where-object {$_.href -match 'CL9DFMDE'}| select -First 1 -ExpandProperty 'href'

$file= $link 

$filename= [string]($file).substring(122)

$filename

$ie.Navigate("$file")
4

2 回答 2

2

我看了一遍,唯一的办法就是假装“点击”保存按钮。我用以下代码做到了这一点:

#------------------------------
#Wait for Download Dialog box to pop up
Sleep 10
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')

#------------------------------
#Wait for Download to complete
Sleep 10
while($ie.Busy){Sleep 1} 
#------------------------------
于 2015-01-21T18:13:19.900 回答
0

您可以使用 Invoke-WebRequest 来保存文档,如下所示:

Invoke-WebRequest -Uri "URL_OF_Download_file" -Outfile "save_location"
于 2016-04-12T13:37:40.103 回答