1

Struggling with the following.

See the extract of the HTML, the ID is present but it keeps returning a null expression. Unfortunately I cant give any more details on the website... Anything obvious to what I'm doing wrong?

--- Extract of HTML ----

<input name="ctl00$mainCopy$LoginView$LoginControl$UserName" type="text" id="ctl00_mainCopy_LoginView_LoginControl_UserName" tabindex="1" class="txtBox" onchange="document.getElementById('ctl00_mainCopy_LoginView_LoginControl_PasswordResetLink').disabled
= false;" style="width:150px;" />

--- Script ---

$ie = New-Object -com internetexplorer.application
$ie.visible = $true
$ie.navigate("excluded for security")

$ie.document.getElementByID("ctl00_mainCopy_LoginView_LoginControl_UserName").Value= "excluded for security"

---- Error ----

You cannot call a method on a null-valued expression.
At line:1 char:1
+ $ie.document.getElementByID("ctl00_mainCopy_LoginView_LoginControl_UserName").Va ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Community
  • 1
  • 1
Luke Griffith
  • 226
  • 4
  • 14

2 Answers2

0

Try putting a sleep after the $ie.navigate command, such as this:

while ($ie.Busy() -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }

It looks like your page is still loading when you attempt to get an element.

Micky Balladelli
  • 9,781
  • 2
  • 33
  • 31
0

This issue is also addressed here.

I am going to assume that your URL is in a secure Zone such as 'Local intranet' or 'Trusted sites'. To test this, try navigating with your code to www.google.com instead of "excluded for security".

The IE object is opened with an Internet zone context. There are zone elevation security mechanisms in place to prevent a script (rather than a user) to navigate from a security zone to another. Zone Elevation Restrictions

There are multiple solutions to this problem depending on your needs and if you have additional constraints. On such workaround is to start PowerShell as Admin which in turn opens IE as Admin. Other solutions by MS.

Hope this helps.

Community
  • 1
  • 1
user2704504
  • 133
  • 6