【问题标题】:powershell click on internet explorer popuppowershell 点击 Internet Explorer 弹出窗口
【发布时间】: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


【解决方案1】:

继续我的评论。

其他人遇到了这个对话框和其他人,并且如前所述,使用 Selenium、AutoIT 等来处理这个问题;而其他人则尝试了不同的方法。

For Example:

# using the process handle of that dialog
$ws = New-Object -ComObject WScript.Shell
$ld = (gps iex* | where {$_.MainWindowTitle }).id

if($ld.Count -gt 1)
{
  $ws.AppActivate($ld[1])
  $ws.sendkeys("{ENTER}")
}

# Using the WASP module 
# (note - though the code for this module is still available, the DLL is not. So, you have to compile that yourself.)
Import-Module WASP
while ($true) {
  [System.Threading.Thread]::Sleep(200) 
  $confirmation = Select-Window iexplore 
  if ($confirmation -ne $null) 
  {
    Select-ChildWindow  -Window $confirmation | 
    Select-Control -title "OK" -recurse | 
    Send-Click
  }
}

顺便说一句..

“按回车键也会关闭弹出窗口”

...这是因为模态对话框在被关闭之前总是占据焦点。

【讨论】:

    猜你喜欢
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多