我正在尝试自动登录并从特定网站收集数据。这是我的代码
$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()方法。但由于某种原因,我仍然没有打电话给它。有人可以就此事向我提出建议吗?
答案 0 :(得分:3)
我已成功使用此代码
成功调用Click()方法$ie.Document.getElementsByName('submit')[0].Item().Click();
仍然不明白我将使用Item属性的原因。现在一切正常。非常感谢解释为什么在这种情况下使用Item属性。