internet-explorer - 方法调用失败,因为 [System.Object[]] 不包含名为 'Click' 的方法

标签 internet-explorer powershell com

我正在使用 PowerShell 运行以下代码,它在一台服务器上成功运行,但在另一台服务器上运行失败。两台服务器都运行带有 IE 8 的 Windows Server R2 Standard。脚本以管理员模式运行,同样在 IE 中,本地 Intranet 的“启用保护模式”已关闭。

$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true 
$ie.Navigate("http://localhost/testwebsite")
While ($ie.Busy) {Sleep 3}
$doc = $ie.Document
$btn = $doc.getElementsByTagName("input")
$Button = $btn | ? {$_.Name -eq "refreshBtn"}
$Button.Click()
$ie.Quit()

这是我在其中一个盒子上遇到的错误(注意:不要在另一台机器上收到此错误):

Method invocation failed because [System.Object[]] doesn't contain a method named 'Click'.

我需要在服务器上更改安全设置吗?我需要调整脚本吗?还有什么吗?

顺便说一句:我已经检查了 StackOverflow 上有关与此相关的问题的各种帖子,但到目前为止我还没有找到任何有用的东西。

提前致谢!

最佳答案

不确定为什么不同,但它看起来像您的管道

$Button = $btn | ? {$_.Name -eq "refreshBtn"}

返回多个按钮,所以 $Button 实际上是一个数组。 PowerShell 3 更好地处理了这个问题:它实际上会对数组中的每个元素调用 Click()。升级到 PowerShell 3 可能不是一种选择。

您可以通过多种方式解决此问题。首先,将 Click() 方法调用添加到找到按钮的管道中:

$doc.getElementsByTagName("input") | 
    Where-Object { $_.Name -eq "refreshBtn" } | 
    ForEach-Object { $_.Click() }

当然,如果返回多个按钮,您可能会认为这是一个错误。在这种情况下,您需要处理它:

$Button = $doc.getElementsByTagName("input") | 
              Where-Object {$_.Name -eq "refreshBtn"}
if( $Button -is 'Object[]' )
{
    Write-Error ('Found multiple <refreshBtn> buttons.')
}

关于internet-explorer - 方法调用失败,因为 [System.Object[]] 不包含名为 'Click' 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17495448/

相关文章:

javascript - IE 中每个浏览器选项卡中的不同图标

inheritance - COM 接口(interface)版本控制和继承

powershell - 从两个对象创建对象的最佳方式

c# - 从 Python 运行 .NET COM 程序集。程序集 dll 的 Python 路径?

.net - 在不使用 GAC 的情况下向 COM 注册托管程序集

html - IE 中的边界半径和 RTL 问题

google-chrome - Chrome 要求对我打开的每个页面进行代理身份验证,IE 不会

html - IE padding-right issue with bg image in input

powershell - 向外部程序发送多个命令

powershell - 使用Powershell在Excel中同时选择多个范围