首页 > 解决方案 > 使用 PowerShell 在 Internet Explorer 中按“另存为”按钮

问题描述

我无法使用 PowerShell 按“另存为”按钮。我尝试在 Internet Explorer 选项中找到“不提示保存”选项,但它不存在。也试过使用这个命令,它也不起作用。 $wshell = new-object -com wscript.shell $wshell.AppActivate("Internet Explorer") $wshell.sendkeys("{TAB}") $wshell.sendkeys("{Enter}") 下载选项

我还可以尝试做什么?

标签: powershellinternet-explorer

解决方案


当我们自动化 IE 下载网站上的文件时,会出现下载弹出窗口。我们可以先激活 IE 窗口并使用AppActivate将其置于最前面,然后使用SendKeys发送击键Ctrl+S来保存文件。

您可以参考下面的示例代码:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("https://www.example.com/download.html") #change it to your own url
while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
$link=$ie.Document.getElementById("btnDowload") #change it to your own selector
$link.click()

Sleep 5
$ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id) #active IE window
[System.Windows.Forms.SendKeys]::Sendwait("%{s}"); #press Ctrl+S to save file

推荐阅读