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
LVL 8
Leo TorresSQL DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.