无法转换

时间:2015-05-22 01:26:50

标签: powershell scripting

语言:Powershell

用法:登录网站

代码: (注意:尚未提供第1行,第2行和第4行的实际值)

$username = "User"
$password = "Password" 
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("URL")
$ie.visible =$true
$doc = $ie.document

$doc.getElementByID("username").value=$username
$doc.getElementByID("password").value=$password
$link = @($doc.getElementsByTagName('A')) | where-object {$_.innerText -eq 'Log In'}
$link.click

第1行至第6行完美运行,并将默认浏览器启动到指定网址的登录页面。

第7行至第9行产生以下错误:

第7行:

Exception calling "getElementById" with "1" argument(s): "Unable to cast 

COM object of type 'mshtml.HTMLDocumentClass' to interface type 

'mshtml.DispHTMLDocument'. This operation failed because the 

 QueryInterface call on the COM component for the interface with IID 

'{3050F55F-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following 

error: No such interface supported (Exception from HRESULT:

0x80004002 (E_NOINTERFACE))."

At line:1 char:20
+ $doc.getElementByID <<<< ("username").value=$username
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

第8行:

Exception calling "getElementById" with "1" argument(s): "Unable to cast 

COM object of type 'mshtml.HTMLDocumentClass' to interface type 

'mshtml.DispHTMLDocument'. This operation failed because the   

QueryInterface call on the COM component for the interface with IID 

'{3050F55F-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following 

error: The RPC server is unavailable. (Exception from HRESULT: 

0x800706BA)."

At line:1 char:20
+ $doc.getElementByID <<<< ("password")
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

第9行:

Exception calling "getElementsByTagName" with "1" argument(s): "Unable to 

cast COM object of type 'mshtml.HTMLDocumentClass' to interface type 

'mshtml.DispHTMLDocument'. This operation failed because the 

QueryInterface call on the COM component for the interface with IID 

'{3050F55F-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following 

error: The object invoked has disconnected from its clients. (Exception 

from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))."

At line:1 char:36

+ $link = @($doc.getElementsByTagName <<<< ('A')) | where-object   
{$_.innerText -eq 'Log In'}

+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

我不确定这个错误告诉我什么,需要一些Powershell Gurus的建议。基于Powershell的使用,我无法在堆栈中找到任何涉及此问题的内容。 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果在查看文档之前放置睡眠会怎样?

do { Sleep 1 } while( $ie.Busy -or $ie.ReadyState -ne 4 )
像这样

$ie.visible =$true
do{Sleep 1} while( $ie.Busy -or $ie.ReadyState -ne 4 )
$doc = $ie.document