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

标签 powershell internet-explorer

我正在尝试自动登录并从特定站点收集数据。这是我的代码

$ie = New-Object -ComObject InternetExplorer.Application;
$ie.Visible = $true;
$ie.Navigate("somesite");
while($ie.busy){Start-Sleep 1;}
while($ie.ReadyState -ne 4){Start-Sleep 1;}
if($ie.Document -ne $null)
{
    $usertextbox = $ie.Document.GetElementById('username');
    $passtextbox = $ie.Document.GetElementById('password');
    $usertextbox.value = "$user";
    $passtextbox.value = "$pass";
    $okbutton = $ie.Document.getElementsByName('submit')[0];
    $okbutton.Click($false);
}

不幸的是,我收到以下错误消息

Method invocation failed because [System.__ComObject] does not contain a method named 'Click'.
At line:17 char:5
+     $okbutton.Click($false);
+     ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Click:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MethodNotFound

您可以在下面看到我使用的元素的 HTML 代码

<input name="submit" accesskey="l" value="Login" tabindex="4" type="submit" />

当我在 $okbutton 上使用 gm 时,我得到了这个

PS C:\Windows\system32> $okbutton | gm


   TypeName: System.__ComObject#{3050f57d-98b5-11cf-bb82-00aa00bdce0b}

Name                         MemberType Definition                                                     
----                         ---------- ----------                                                     
addBehavior                  Method     int addBehavior (string, Variant)                              
addFilter                    Method     void addFilter (IUnknown)                                      
appendChild                  Method     IHTMLDOMNode appendChild (IHTMLDOMNode)                        
applyElement                 Method     IHTMLElement applyElement (IHTMLElement, string)               
attachEvent                  Method     bool attachEvent (string, IDispatch)                           
blur                         Method     void blur ()                                                   
clearAttributes              Method     void clearAttributes ()                                        
click                        Method     void click ()

据我所知,这个元素肯定而且必须包含 click() 方法。但出于某种原因,我仍然无法调用它。谁能就此向我提出建议?

最佳答案

我已经成功地使用这段代码调用了 Click() 方法

$ie.Document.getElementsByName('submit')[0].Item().Click();

仍然不明白我要使用 Item 属性的原因。现在一切正常。对于在这种情况下为什么要使用 Item 属性的解释,我们将不胜感激。

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

相关文章:

powershell - 如何在 Azure Functions 中将 GET 与 PowerShell 结合使用?

javascript - 的意思 !!在 JavaScript 中

javascript - 使用 Internet Explorer 10 及以下版本保存 Base64 编码的 PDF

powershell - 合并超大文本文件的最佳方法

.net - Powershell 调用使用 App.config 的 .NET 程序集

.net - PowerShell - 如果用户输入凭据,则查询 AD 时出错

javascript - Internet Explorer 怪异 - function item() { [native code] } - 它有什么作用?

powershell - Azure New-AzureRmResource "Account Type"丢失,但如果我添加它 "Parameter not found"

jquery - IE旋转离轴

css - 为什么 IE11 会随机执行不合适的媒体查询?