【问题标题】:Method invocation failed because [System.__ComObject] does not co ntain a method named 'Click'方法调用失败,因为 [System.__ComObject] 不包含名为“Click”的方法
【发布时间】:2016-07-27 14:29:31
【问题描述】:

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

$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() 方法。但由于某种原因,我仍然无法调用它。有人可以就这件事给我建议吗?

【问题讨论】:

  • 作为评论而不是答案发布,因为我还没有使用 PoSh 进行任何 COM 自动化,但我很确定这只是一个错字。在您的代码中,您调用 .Click() 方法。错误消息指的是“点击”,而您对 get-member 的调用拼写为“点击”。看出区别了吗?
  • 试过点击,但这没有帮助。 btw gm 显示 void click(),这意味着 Click() 据我所知

标签: powershell internet-explorer


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-05-31
    • 2013-07-03
    • 1970-01-01
    • 2018-11-27
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多