【问题标题】:Powershell download file from website ie.document methodPowershell从网站下载文件ie.document方法
【发布时间】:2014-09-24 18:47:59
【问题描述】:

我创建了一个使用 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")

【问题讨论】:

  • 有什么办法绕过ie11弹出提示保存打开?

标签: automation download powershell-3.0 powershell-4.0


【解决方案1】:

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

#------------------------------
#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} 
#------------------------------

【讨论】:

    【解决方案2】:

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

    Invoke-WebRequest -Uri "URL_OF_Download_file" -Outfile "save_location"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      相关资源
      最近更新 更多