【发布时间】:2021-08-26 22:02:28
【问题描述】:
我需要使用 PowerShell 在打开 Internet Explorer 时出现的这个弹出窗口上关闭。按回车键也会关闭弹出窗口。
我的尝试
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$ie = new-object -com internetexplorer.application
$ie.visible = $true
$ie.navigate('http://website/')
while ($ie.busy) { Start-Sleep 3 }
[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}");
Start-Sleep 3
$link = $ie.Document.getElementsByTagName('Button') | where-object { $_.innerText -eq 'Simple Setup' }
$link.click()
Start-Sleep 2
$ie.quit()
【问题讨论】:
-
这不是一个网页,它是一个 WIndows 模式对话框。所以,在对话框上使用 IE COM 不是一回事。您必须通过 UX/UI 自动化直接对模态对话框进行操作。 使用 sendkeys/selenium/AutoIt 切换到模态对话框,然后在您登陆按钮时发送回车键,或者捕获对话框的 Windows 句柄并执行操作关于那个。
-
我同意 postanote 的评论。这是一个操作系统级别的窗口,您无法使用 IE COM 访问它。你说敲回车键可以关闭它,那么你可以尝试使用
SendKeys来模拟回车键点击。在PowerShell中使用SendKeys可以参考this thread。
标签: powershell internet-explorer powershell-remoting