• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2180
  • Last Modified:

Powershell login to website

I dont think I have the correct tags here


CLS
$url = "www.SomeSite.com"
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $true 
$ie.silent = $true 
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1} 
#$secLink = $ie.Document.getElementsByTagName('A') | Where-Object {$_.innerText -eq 'Continue to this website (not recommended).'} 
#$secLink.click() 
$ie.Document.getElementsByType("input") | where { $.Name -eq "loginInput" }.value = "UserName"
$ie.Document.getElementsByName("input") | where { $.Name -eq "passwordField" }.value = "password"
$loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'Login'} 
$loginBtn.click() 

Open in new window


Error I get
Method invocation failed because [mshtml.HTMLDocumentClass] does not contain a method named 'getElementsByType'.
At line:10 char:1
+ $ie.Document.getElementsByType("input") | where { $.Name -eq "loginInput" }.valu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
Where-Object : Cannot bind argument to parameter 'FilterScript' because it is null.
At line:11 char:49
+ $ie.Document.getElementsByName("input") | where { $.Name -eq "passwordField" }.v ...
+                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Where-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.WhereObjectCommand
 
Method invocation failed because [mshtml.HTMLDocumentClass] does not contain a method named 'getElementsById'.
At line:12 char:1
+ $loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'b ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
You cannot call a method on a null-valued expression.
At line:13 char:1
+ $loginBtn.click()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Open in new window


Here is the HTML tags I am looking at to try and log in. Not sure what I am doing wrong
HTML
0
Leo Torres
Asked:
Leo Torres
  • 5
  • 3
1 Solution
 
QlemoC++ DeveloperCommented:
Two mistakes.
1. You've used getElementsByType instead of getElementsByName once
2. You can't request a .value from a scriptblock - you intended to apply that to the result of where:
CLS
$url = "www.SomeSite.com"
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $true 
$ie.silent = $true 
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1} 
#$secLink = $ie.Document.getElementsByTagName('A') | Where-Object {$_.innerText -eq 'Continue to this website (not recommended).'} 
#$secLink.click() 
($ie.Document.getElementsByName("input") | where { $.Name -eq "loginInput" }).value = "UserName"
($ie.Document.getElementsByName("input") | where { $.Name -eq "passwordField" }).value = "password"
$loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'Login'} 
$loginBtn.click() 

Open in new window

Maybe you have to use GetElementsById instead of GetElementsByName.
0
 
Leo TorresSQL DeveloperAuthor Commented:
Code produced this error.

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At line:12 char:1
+ ($ie.Document.getElementsByName("input") | where { $.Name -eq "loginInput" }).va ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At line:13 char:1
+ ($ie.Document.getElementsByName("input") | where { $.Name -eq "passwordField" }) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
Method invocation failed because [mshtml.HTMLDocumentClass] does not contain a method named 'getElementsById'.
At line:14 char:1
+ $loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'b ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
You cannot call a method on a null-valued expression.
At line:15 char:1
+ $loginBtn.click()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Open in new window

0
 
Leo TorresSQL DeveloperAuthor Commented:
There is no value inside the node. I believe that is what is causing error.  loginInput is the text field that holds User name text but there is no Value tag. Could it be we need to use id="loginfield" ?
0
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
QlemoC++ DeveloperCommented:
I'm using this sequence to log in:
$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'username' } | % { $_.value = "MyUsername" }                                                              
$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'password' } | % { $_.value = "MyPassword" }
$ie.Document.getElementsByTagName("input") | ? { $_.Type -eq 'submit' } | % { $_.Click() }  

Open in new window

and it works great. But that depends on the site we query.
0
 
Leo TorresSQL DeveloperAuthor Commented:
OK now we are getting somewhere
The code Populates username and password. but it wont press submit button
there is no .type there is an <a onclick..

See image here for submit

submit
0
 
Leo TorresSQL DeveloperAuthor Commented:
OK was able to get pass that first button

with this code
$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'loginField' } | % { $_.value = "User" } 
$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'passwordField' } | % { $_.value = "pass" }
$ie.Document.getElementsByTagName("button") | ? { $_.Type -eq 'button' } | % { $_.Click() } 

Open in new window

But I need to click this button and its a bit different. I need to press Go button NOT Cancel button.

Currently I have this command and it just sits on page
 
$ie.Document.getElementsByTagName("button") | ? { $_.Type -eq 'button' -and $_.name -eq "Go" } | % { $_.Click() }

Open in new window

Go Button
I think its because there are other buttons on page. Could it be the other buttons are ALL buttons
0
 
QlemoC++ DeveloperCommented:
It's hard to tell without seeing the HTML code, but I guess
$ie.Document.getElementsByTagName("button") |
   ? { $_.Type -eq 'button' -and $_.name -eq "Go" } |
   % { $_.Click() }

Open in new window

does not find anything, and Click() will not be executed. If the script would find several buttons, all would be "clicked". You should cross-check with
$ie.Document.getElementsByTagName("button") | select Type, Name, ID

Open in new window

while past the login page.
0
 
Leo TorresSQL DeveloperAuthor Commented:
The Select only returned Type button the name and ID was blank

Think I got passed this question.

can you help me in the next phase
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28494068.html

Thank you!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now