方法调用失败,因为[System.__ComObject]不包含名为“Click”的方法。

3

我正在尝试自动登录并收集特定网站的数据。以下是我的代码:

$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()方法。错误消息引用了“Click”,而您的get-member调用拼写为“click”。看到区别了吗? - Eric Walker
尝试了点击但没有帮助。顺便说一下,gm显示void click(),据我所知这意味着Click()。 - KennyMacCormik
1个回答

5
我已成功使用以下代码调用Click()方法。
$ie.Document.getElementsByName('submit')[0].Item().Click();

我还不明白为什么要使用Item属性。现在一切都正常工作了。如果能解释一下为什么在这种情况下应该使用Item属性,将不胜感激。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接