Unable to login to a website via powershell - powershell

I am trying to open a url in IE and then pass username and password to login to the url.But my code is not filling username and password
Following is code
$username = "userhere"
$password = "passhere"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$false
$ie.navigate("https://ameriprisestage.service-now.com/")
#while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("user_name").value= "$username"
$ie.document.getElementById("user_password").value = "$password"
Error: You cannot call a method on a null-valued expression. At line:7 char:1 + $ie.document.getElementById("user_name").value= "$username" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression. At line:8 char:1 + $ie.document.getElementById("user_password").value = "$password" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

There is a couple of things to note. First, your page is not ready yet. Yuo must wait for load. Your commented line was intended to do that. Second, both fields are in iframe which works as a separate sandbox - has his own document object. I also removed window hiding code to see results. Here is working code:
$username = "userhere"
$password = "passhere"
$ie = New-Object -com InternetExplorer.Application
$ie.navigate("https://ameriprisestage.service-now.com/")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById('gsft_main').contentWindow.document.getElementById('user_name').value = $username
$ie.document.getElementById('gsft_main').contentWindow.document.getElementById('user_password').value = $password
Make sure you close $ie object. You may have tons of them as background processes.

Related

PowerShell: Auto Login to a website using MS Edge instead of IE throws errors

Courtesy of this post, the following code auto-logs into a URL using MS IE and works great. The first two lines are the most important here. See below for what happens when I change them. I want to make it clear That I am not a professional web developer. I am only a system administrator forced to hack my way along when it comes to coding.
Working code when PowerShell calls IE:
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$username="myname"
$RSAPIN="mypin"
$password="mypassword"
$ie.Navigate("https://www.tibia.com/mmorpg/free-multiplayer-online-role-playing-game.php")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('username')
$usernamefield.value = "$username"
$RSAPINfield = $ie.document.getElementByID('password_input')
$RSAPINfield.value = "$RSAPIN"
$passwordfield = $ie.document.getElementByID('secondary_password_input')
$passwordfield.value = "$password"
Non-working code is shown below; If I change the code to call MS Edge instead, I get a raft of errors and I think it's because PowerShell New-Object doesn't support MS Edge?
$msedge = New-Object -ComObject 'internetExplorer.Application'
$msedge.Visible= $true # Make it visible
$username="myname"
$RSAPIN="mypin"
$password="mypassword"
$msedge.Navigate("https://www.tibia.com/mmorpg/free-multiplayer-online-role-playing-game.php")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $msedge.document.getElementByID('username')
$usernamefield.value = "$username"
$RSAPINfield = $msedge.document.getElementByID('password_input')
$RSAPINfield.value = "$RSAPIN"
$passwordfield = $msedge.document.getElementByID('secondary_password_input')
$passwordfield.value = "$password"
Errors seen when calling MS Edge instead of IE:
New-Object : Retrieving the COM class factory for component with CLSID
{00000000-0000-0000-0000-000000000000} failed due to the following
error: 80040154 Class not registered (Exception from HRESULT:
0x80040154 (REGDB_E_CLASSNOTREG)). At
C:\Scripts\msedge.ps1:3
char:11
$msedge = New-Object -ComObject 'msedge.Application'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
The property 'Visible' cannot be found on this object. Verify that the
property exists and can be set. At
C:\Scripts\msedge.ps1:4
char:1
$msedge.Visible= $true # Make it visible
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound You cannot call a method on a null-valued expression. At
C:\Scripts\msedge.ps1:8
char:1
$msedge.Navigate("myurl")
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
C:\Scripts\msedge.ps1:12
char:1
$usernamefield = $msedge.document.getElementByID('username')
I am at a loss here. According to this post, PowerShell New-Object only supports IE's COM Automation.
You can't automate the Edge Chromium browser like what you do with IE using PowerShell script. The COM automation interface are for IE, so it doesn't work with Edge. Besides, Edge doesn't have such interface.
It's recommended to use Selenium WebDriver to automate Edge. Selenium web driver supports many developing languages, you can choose the desired language.
You can also refer to the official doc of Selenium to learn how to get start with Selenium and refer to the code sample of different languages.

Website Login Automation and Output STatus

I am trying to automate a task to login and verify or not login is success in a nutshell I am able to create.
$username = "XXXX"
$password = "XXXXXXXX"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("https://com/login")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("user_email").value= "$username"
$ie.document.getElementById("user_password").value = "$password"
$ie.document.getElementById("Loginform").submit()
start-sleep 20
$ie.Document.body
Its throwing error
Exception from HRESULT: 0x800A01B6
+ $ie.document.getElementById("user_email").value= "$username"
At line:8 char:1
+ $ie.document.getElementById("user_password").value = "$password"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException
Exception from HRESULT: 0x800A01B6
At line:9 char:1
i can confirm that the HTML id for the login box is user_email and user_password

Why document object is null-value in my powershell script?

I want to use powershell to automate a web download request. But I got some errors that I can't fix.
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.visible = $true
$ie.Navigate("http://10.8.140.232/KPIReport/Admin/Login.aspx")
$usr_name = $ie.document.getElementById('TextBoxAccount')
$pwd = $ie.document.getElementById('TextBoxPassword')
$login_button = $ie.document.getElementById('ButtonLogin')
the first 3 lines can run successfully.
but from line 4, error occurs.
You cannot call a method on a null-valued expression.
At line:4 char:1
+ $usr_name = $ie.document.getElementById("TextBoxAccount")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
And $ie.document return null in command line.
and by the way. all the element id are existed in source html code.
I don't what happens here.
thanks for your help.
As the comment above, the error is caused by the DOM hasn't been fully loaded. I use While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;} after $ie.Navigate. You could try to add it in your code.

Login credentials to website using powershell v5

I am new to powershell and I am trying to figure out how to insert my username and password into a website to login. Below is what I have come up with but failing to get it to work. Also the username and password are AD accounts if that matters.
URL of the site that will be launched.
$Url = “http://www.example.com”
$title = "User Authentication Required"
$message = "Enter user credentials for example.com"
$user = ""
$user_creds = $host.ui.PromptForCredential($title, $message, $user, "")
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true;
$IE.navigate($url);
Wait a few seconds.
while ($IE.Busy -eq $true)
{Start-Sleep -Milliseconds 2000;}
The following UsernameElement, PasswordElement, and LoginElement need to be modified first.
# of the script for more details.
$IE.Document.getElementById(“userid”).value = $User_creds.Username
$IE.Document.getElementByID(“password”).value = $User_creds.password
$IE.Document.getElementById(“button-1034-btnIconEl”).Click()
This is the error I am getting now...
The property 'value' cannot be found on this object. Verify that the property
exists and can be set.
At C:\logon test.ps1:27 char:1
+ $IE.Document.getElementById("userid").value = $User_creds.Username
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'value' cannot be found on this object. Verify that the property
exists and can be set.
At C:\logon test.ps1:28 char:1
+ $IE.Document.getElementByID("password").value = $User_creds.password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
If I run this in v2 it still does not input the username but for the password it gives an error of System.Management.Automation. The enter code to login is working. Can someone please explain how can I get a username and password to input into the website page and the password needs to be hidden/secured.

Powershell to press a button in Explorer

I have a web page with the following code relevant to the button:
<td width=300 align=left>Click here to run the batch tool:</td>
<td align='left' width=100><input id="Submit1" type="submit" value="Run" onClick='showSts();'/></td>
And my attempts to programmatically press it from Powershell using this code:
$username='username' $password='password'
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("name of Web site")
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
# $usernamefield = $ie.Document.getElementByID('memberemail')
# $usernamefield.value = $username
# $passwordfield = $ie.Document.getElementByID('memberpassword')
# $passwordfield.value = $password
$submitebutton=$ie.Documentelement.getElementByclassname('submit1') | Select-Object -First
$submitebutton.click()
Gives this error:
You cannot call a method on a null-valued expression. At
C:\Users\zkf4bi4\Documents\houdini_click.ps1:15 char:1
+ $submitebutton=$ie.Documentelement.getElementByclassname('submit1') | Select-Ob ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
C:\Users\zkf4bi4\Documents\houdini_click.ps1:16 char:1
+ $submitebutton.click()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any ideas on how to proceed? I'm an experienced developer but am new to Powershell
Well, I can't help but notice that you're calling GetElementsByClassName but 'Submit1' is the elements id. I suspect the error message is trying to tell you that $submitebutton is null. You might find this question an almost exact duplicate.