Link to home
Start Free TrialLog in
Avatar of Leo Torres
Leo TorresFlag for United States of America

asked on

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
User generated image
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of Leo Torres

ASKER

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

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" ?
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.
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

User generated image
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

User generated image
I think its because there are other buttons on page. Could it be the other buttons are ALL buttons
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
https://www.experts-exchange.com/questions/28494068/Powershell-get-href-from-page-and-assign-to-variable.html

Thank you!