【问题标题】:Automating IE confirmation prompt with Powershell使用 Powershell 自动化 IE 确认提示
【发布时间】:2019-02-05 23:58:48
【问题描述】:

我有一个很好的 powershell 脚本,它可以为我女儿自动化一个特定的网站。最近他们更改了站点并添加了一个不错的新功能,可以将我的脚本速度提高 10 倍。问题是他们用来激活它的输入类型会弹出一个确认对话框。 HTML 看起来像这样

<input type=submit name='enter_all' value='Enter all' onClick="return confirm('Enter all?');">

我可以在 powershell 中自动回答这个对话框吗?我希望脚本在后台运行,所以我认为发送击键已经过时了。

【问题讨论】:

  • 到目前为止你的自动化程度如何?
  • 使用 com 和 Internet Explorer,就像 stej 下面给出的示例一样。使用 IE com 对象,我可以填写表单、单击值等。在我遇到这个弹出确认提示之前工作得很好。

标签: internet-explorer powershell automation


【解决方案1】:

我不会费心去寻找如何点击确认对话框,只处理 COM 看起来相当困难。

相反,我会像这样简单地提交表单。假设我有这个 HTML:

<html>
  <body>
  <form action="http://localhost/xx">
  <input type="submit" onclick="return confirm('really?')" value="ENTER" />
  </form>
  </body>
</html>

我可以使用此代码绕过确认对话框:

$ie = New-Object -ComObject internetexplorer.application
$ie.Navigate('http://localhost/x.html')
$ie.Visible = 1 # not needed
# look for the correct form, you probably know which one is it
$ie.document.forms | 
   Select -First 1 | 
   % { $_.submit() }   # simply submit...

【讨论】:

  • 我得试试。我最终做了一些更复杂的事情。我从 powershell 脚本启动了另一个线程,并使用 WASP 搜索表单并单击该线程中的按钮。所以一个线程只关注这个表单并点击按钮。
  • 我试过了,但我无法让它工作。提交执行但似乎没有做任何事情。表单有不止一种提交的输入类型,这可能是问题吗?
  • 是的,这可能是个问题。但如果没有完整的 HTML 或对页面的访问,就很难再提供任何建议了..
  • 我可以获取完整的 HTML,但它非常大,并且无法访问,因为需要登录。我想出的 hack 工作可靠,所以我将把它留在原处。感谢您的回复。
【解决方案2】:

我有一个更简单的解决方案。您可以使用自己的覆盖本机 window.confirm 函数。我在下面的实现将其替换为 confirm 函数,该函数只会绕过一次确认对话框。

PowerShell:

$ie = New-Object -COM InternetExplorer.Application -Property @{
    Navigate = "http://www.example.com/"
    Visible = $true
}
do { Start-Sleep -m 100 } while ( $ie.busy )

# Override window.confirm function with confirmOnce, so that on first call
# it will return true and immediately restore it to native confirm.
$jsCommand = @"
    window.confirm = (function() {
        var nativeConfirm = window.confirm;
        function confirmOnce(message) {
            window.confirm = nativeConfirm;
            return true;
        }
        return confirmOnce;
    })();
"@
$document = $ie.document
$window = $document.parentWindow
$window.execScript($jsCommand, 'javascript') | Out-Null

运行脚本后,将打开一个 IE 窗口。按 F12 转到 JavaScript 控制台并输入:

confirm('Really?')

它将返回true。再次尝试运行confirm,会打开原来的确认对话框。

【讨论】:

    【解决方案3】:

    回答我自己的问题,但我确实找到了一种方法来做到这一点,即使它有点像黑客。

    主脚本通过 Com 使用 Internet Explorer,很像

    $ie = new-object -com "InternetExplorer.Application"
    

    关于如何使用ie com对象进行导航、填写字段、单击按钮等的例子很多。但是弹出确认只会导致事情挂掉。

    为了解决这个问题,我使用Wasp 启动了另一个线程,这是一个用于自动化 Windows 窗体的库。在这个线程中,我只是坐在一个循环中寻找确认提示并单击它。 (在我的情况下,窗口会弹出很多次)。该脚本的代码如下所示

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

    【讨论】:

      【解决方案4】:
      $ws = New-Object -ComObject WScript.Shell
      
      $ld = (gps iex* | where {$_.MainWindowTitle }).id
      
      if($ld.Count -gt 1)
      {
        $ws.AppActivate($ld[1])
      
        $ws.sendkeys("{ENTER}")
      }
      

      【讨论】:

      • 添加一些解释是个好主意,这样其他人就会知道为什么您的代码比其他答案更好,因此值得投票...使用edit 添加文本。
      猜你喜欢
      • 2013-07-15
      • 1970-01-01
      • 2017-07-26
      • 2010-10-10
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      相关资源
      最近更新 更多