Unable to Cast COM Object of type

0

Language: Powershell

Usage: Login into Website

Code: (NOTE: Actual values for lines 1., 2., and 4. have not been provided)

$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

Lines 1 thru 6 work perfectly and launch the default browser to the Login Page of the specified URL.

Lines 7 thru 9 produce the following errors:

Line 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

Line 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

Line 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

I am not sure what this error is telling me and need some suggestions from some Powershell Gurus out there. I could not find anything in the stack that has this issue covered based on the use of Powershell. Thanks in advance for any help.

powershell
scripting
asked on Stack Overflow May 22, 2015 by James

1 Answer

1

What happens if you put sleep before looking into the document?

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

like this

$ie.visible =$true
do{Sleep 1} while( $ie.Busy -or $ie.ReadyState -ne 4 )
$doc = $ie.document
answered on Stack Overflow May 22, 2015 by hysh_00

User contributions licensed under CC BY-SA 3.0