0

Here is a PowerShell script to trigger Internet Explorer, open LinkedIn login page and enter some text in the username text field.

$ie = New-Object -Com "InternetExplorer.Application"
$ie.Navigate("www.linkedIn.com")
$ie.Visible = $true

$doc = $ie.document
$usernameElement = $doc.getElementByTagName("input") | Where-Object {$_.id = "session_key-login""}
$usernameElement.Click()

Get-Process iexplore | Foreach-Object {$_.CloseMainWindow()}

Unfortunately, I keep getting the following error:

You cannot call a method on a null-valued expression.
At C:\Users\Pinku\Desktop\Untitled1.ps1:7 char:23
+ $usernameElement.Click <<<< ()
+ CategoryInfo          : InvalidOperation: (Click:String) [], RuntimeExcepti 
on
+ FullyQualifiedErrorId : InvokeMethodOnNull

I have tried but have not been able to alleviate myself from this issue.Please suggest!

Praveen Pandey
  • 658
  • 3
  • 16
  • 32

1 Answers1

0

Instead of using $doc.getElementsByTagName("input") and then trying to filter through the results, try retrieving the ID directly using getElementById:

$usernameElement = $doc.getElementById("session_key-login")
$usernameElement.Click()

---Edit---

Response to still getting the null-valued expression after using the above:

The error message is that it can't find any elements called "session_key-login", and so it returns $null, and hence, when you try to invoke the Click() method, it throws the error. Some things to try:

-Check to see if the id exists. Run the following code after creating your $ie object, and see if there is an ID that matches "session_key-login":

$ie = New-Object -Com "InternetExplorer.Application"
$ie.Navigate("www.linkedIn.com")
$ie.Visible = $true

$doc = $ie.document
$doc.getElementsByTagName("Input") | Select Id, Name

-Try running your PowerShell session as Administrator. I know I wasn't able to launch IE properly until I ran PowerShell as Administrator. For ex. even though the iexplore process was created, the physical Internet Explorer window didn't open for me.

HAL9256
  • 12,384
  • 1
  • 34
  • 46
  • Hi, thanks for your response but I have already tried your solution. I still get the same error. – Praveen Pandey Feb 21 '14 at 18:38
  • I get this post your suggestion-`You cannot call a method on a null-valued expression. At C:\Users\Pinku\Desktop\Untitled1.ps1:6 char:45 + $usernameElement = $doc.getElementsByTagName <<<< ("Input") | Select Id, Name + CategoryInfo: InvalidOperation: (getElementsByTagName:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At C:\Users\Pinku\Desktop\Untitled1.ps1:7 char:23 + $usernameElement.Click <<<< () + CategoryInfo: InvalidOperation: (Click:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull` – Praveen Pandey Feb 22 '14 at 07:21
  • Sorry but I am still not clear how to run my script as Administrator. I read to run as admin, I need to write `Start-Process powershell -verb runas -ArgumentList "-file fullpathofthescript"` in my script but then multiple windows open.. :( – Praveen Pandey Feb 22 '14 at 07:23
  • Typically you Right-Click on the PowerShell shortcut and select "Run as Administrator". (If you need to run it from a script see: http://stackoverflow.com/questions/7690994/powershell-running-a-command-as-administrator). Also, run each line of the example I gave in a PowerShell prompt (not from a .PS1 file) so that you can see what is happening (i.e. does Internet explorer actually show up for you? i.e. if IE is not showing up, then the next line: `$doc = $ie.document` will not work, so the issue is with launching the IE process.) – HAL9256 Feb 23 '14 at 04:51