方法调用失败,因为[System .__ ComObject]没有包含名为"Click"的方法 [英] Method invocation failed because [System.__ComObject] does not co ntain a method named 'Click'

查看:622
本文介绍了方法调用失败,因为[System .__ ComObject]没有包含名为"Click"的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to automate logging in and collecting data from specific site. Here is my code

$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);
}

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

Unfortunately I get the following error message

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代码

You can see HTML code of the element I use below

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

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

When I use gm on the $okbutton I get this

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()方法.但是由于某种原因,我仍然无法调用它.有人可以建议我吗?

As far as I see this element is definitely and must contain click() method. But for some reason I'm still failing to call it. Can anyone advise me on that matter?

推荐答案

我已成功使用此代码成功调用Click()方法

I've managed to successfully call the Click() method with this code

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

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

Still don't understand the reason I shall use Item property. Now everything works fine. Will be grateful for the explanation why Item property shall be used in that case.

这篇关于方法调用失败,因为[System .__ ComObject]没有包含名为"Click"的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆