【问题标题】:Corrupt IE object (IE automation with Powershell)损坏的 IE 对象(带有 Powershell 的 IE 自动化)
【发布时间】:2015-08-31 23:35:22
【问题描述】:

我想使用 Powershell 在 Web 控制台上自动配置接入点。

我在 Win8/IE10 上开发,测试代码时一切正常。 不幸的是,我在开发/测试后收到了“生产系统”。 现在代码应该可以在 Windows Server 2012 R2/IE11 上运行了。

这是我连接和登录接入点的代码(简化版):

$IE = New-Object -ComObject "InternetExplorer.Application"
$IE.Visible = $true #1#
$IE.Navigate("192.168.0.100")
Write-Host $IE.LocationName

$IE.Document.GetElementByID("login-username").Value = "admin"
$IE.Document.GetElementByID("login-password").Value = "admin"
$IE.Document.GetElementByID("login-btn").Click()
Write-Host $IE.LocationName

出于调试原因,我通常会显示窗口(注释 #1#)。 首先,Write-Host 显示正确的位置(“登录”)。 在第二个 Write-Host 处,显示了错误的位置(“about:blank”)。 在 IE 窗口中,您会看到正确的位置/选项卡名称(“AP”)。 似乎 IE 对象变得无法使用,其他属性(如 $IE.Document.*)则不起作用。我必须手动关闭窗口。

我有两种解决方法:

  1. 当我在登录前手动单击 IE 窗口上的(获取焦点)时,它 有效。
  2. 当我不显示窗口时 ($IE.Visible = $false),也 一切正常。

到目前为止我的场景/测试:

  1. IE ESC 已禁用。
  2. 我从另一台生产机器 (2008R2) 复制了 C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll,因此 $IE.Document 函数可以工作(来自我的开发机器的 dll产生同样的错误)。
  3. 我尝试为每个区域启用/禁用保护模式(Internet 选项 > 安全),但对我不起作用(在 reddit 上找到)

其他人有想法尝试吗? 调试适用于解决方法,它也适用于(到目前为止)不可见的窗口 - 但我想了解这些麻烦......

谢谢,最好的问候, kvo

【问题讨论】:

    标签: internet-explorer powershell automation com-object


    【解决方案1】:

    我将脚本部署到在 IE11 上运行的 windows server 2012 后遇到了这个问题。

    造成这种情况的原因是丢失了由 Powershell 创建的 IE 对象的处理程序。

    我使用PowerShell IE9 ComObject has all null properties after navigating to webpage 编写的以下 sn-p 来解决此问题。

    function ConnectIExplorer() {
        param($HWND)
    
        $objShellApp = New-Object -ComObject Shell.Application 
        try {
          $EA = $ErrorActionPreference; $ErrorActionPreference = 'Stop'
          $objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
          $objNewIE.Visible = $true
        } catch {
          #it may happen, that the Shell.Application does not find the window in a timely-manner, therefore quick-sleep and try again
          Write-Host "Waiting for page to be loaded ..." 
          Start-Sleep -Milliseconds 500
          try {
            $objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
            $objNewIE.Visible = $true
          } catch {
            Write-Host "Could not retreive the -com Object InternetExplorer. Aborting." -ForegroundColor Red
            $objNewIE = $null
          }     
        } finally { 
          $ErrorActionPreference = $EA
          $objShellApp = $null
        }
        return $objNewIE
      } 
    
    
    
    
    $HWND = ($objIE = New-Object -ComObject InternetExplorer.Application).HWND
    $objIE.Navigate("https://www.google.com")
    $objIE = ConnectIExplorer -HWND $HWND
    

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2012-03-15
      • 2016-09-30
      • 2014-02-01
      • 1970-01-01
      • 2010-10-10
      相关资源
      最近更新 更多