powershell - 使用 PowerShell 在 Internet Explorer 中按“另存为”按钮
问题描述
我无法使用 PowerShell 按“另存为”按钮。我尝试在 Internet Explorer 选项中找到“不提示保存”选项,但它不存在。也试过使用这个命令,它也不起作用。
$wshell = new-object -com wscript.shell
$wshell.AppActivate("Internet Explorer")
$wshell.sendkeys("{TAB}")
$wshell.sendkeys("{Enter}")
下载选项
我还可以尝试做什么?
解决方案
当我们自动化 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
推荐阅读
- python-requests - 发送标头以发布请求
- javascript - 在纯 Javascript 中使用 Django 权限
- javascript - TypeError: cannot read property 'id' of undefined - Angular 8 Testing
- java - 有什么方法可以读取在 GRID 机器(Selenium-Java)中下载的 csv 文件
- azure-eventhub - 事件中心捕获数据积压
- java - linux环境下无法解压zip文件
- javascript - 只选择一个复选框来切换一组 div
- ionic4 - 如何在侧面菜单离子 4 中获取数据
- excel - 如何仅从特定打开 Excel 文件并从每个 Excel 文件中复制特定单元格值
- vba - 制作打印的 Word 文档的副本并将其保存在文件夹中