PowerShell Exception 0x800A01B6 while using getElementsByTagName, getElementsByName or getElementByID

Recently, I have started writing automation scripts to automate IE websites and make use of the com-object “InternetExplorer.Application” to automate an internet explorer session.

$ie = New-Object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.silent = $true

$ie.Navigate($IURL)
while ($ie.Busy) {
  [System.Threading.Thread]::Sleep(10)
}

Using $ie.Document.getElementsByTagName(“Input”) enables me to enumerate the form fields and buttons. This works for the first website that I am visiting. If I then navigate to another site, $xe.Document.getElementsByTagName(“Input”) generates the following exception:


Exception from HRESULT: 0x800A01B6
At line:1 char:1
+ $Global:ie.Document.getElementsByTagName("Input")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : OperationStopped: (:) [], NotSupportedException
 + FullyQualifiedErrorId : System.NotSupportedException

Workaround

Always use the following methods instead of the native ones:


IHTMLDocument3_getElementsByTagName
IHTMLDocument3_getElementsByName
IHTMLDocument3_getElementByID

Example


$ie = New-Object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.silent = $true
$ie.Navigate($IURL)
while ($ie.Busy)
{
[System.Threading.Thread]::Sleep(10)
}
$ie.Document.IHTMLDocument3_getElementsByTagName("Input") $ie.Navigate($IURL2) while ($ie.Busy)
{
[System.Threading.Thread]::Sleep(10)
}
$ie.Document.IHTMLDocument3_getElementsByTagName("Input")

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s