We help IT Professionals succeed at work.

Powershell login to website

3,656 Views
Last Modified: 2014-08-09
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
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

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

Leo TorresSQL Developer
CERTIFIED EXPERT

Author

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" ?
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

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
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

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
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

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
https://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28494068.html

Thank you!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.