Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Login to a website using powershell

enter image description hereI am getting following error message when i am running my powershell script

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At C:\Test.ps1:17 char:1
+ $usernamefield.value = $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:\Test.ps1:20 char:1
+ $passwordfield.value = $password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Method invocation failed because [System.DBNull] does not contain a method named 'click'.
At C:\Test.ps1:23 char:1
+ $Link.click()
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound`

My script:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible

$usernmae="test"

$password="test1"

$ie.Navigate("https://website.com/login")

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}

$usernamefield = $ie.document.getElementByID('ysi_email')
$usernamefield.value = $username

$passwordfield = $ie.document.getElementByID('ysi_password')
$passwordfield.value = $password

$Link = $ie.document.getElementByID('ysi_btn_login')
$Link.click()

I cant seem to understand the problem here, i have looked into other examples within stackoverflow but i still cant find the problem.

The same id works fine in another example in a python script.

Here is a screenshot Edit: Providing screenshot of the elements

like image 204
irish Avatar asked Nov 16 '16 05:11

irish


3 Answers

The problem comes from the fact that object of IDs 'ysi_email', 'ysi_password' and 'ysi_btn_login' are not found in the DOM of the document loaded at https://website.com/login.

To solve your trouble load your document in Chrome, or Firerfox or Explorer with the developpers tools activated (Press F12) and inspect the objects you want to find.

enter image description here


Here is a working solution according to your comments :

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible

$username="[email protected]"

$password="test1"

$ie.Navigate("https://To your detailled URL")

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}

$usernamefield = $ie.document.getElementByID('ysi_email')
$usernamefield.value = "$username"

$passwordfield = $ie.document.getElementByID('ysi_password')
$passwordfield.value = "$password"

$Link = $ie.document.getElementByID('ysi_btn_login')
$Link.click()

$ie.Quit()

Here you can see the result for me.

enter image description here


enter image description here

like image 143
JPBlanc Avatar answered Oct 19 '22 07:10

JPBlanc


I got stuck for a while with this with IE11 and the native COM methods, I was facing the same problem, there must be some sort of bug, but Selenium with IE11 worked for me, just had to import the .dll and the IE driver, the syntax was similar enough for me to get a hang of it and get what I needed. For whatever reason, selenium found the elements while the native IE COM never did. Also, you can use Chrome or Firefox.

Sample code:

#Load Selenium to download all URL xml sites
Add-Type -Path "C:\Temp\WebDriver.dll" # Load the Selenium .Net library
$env:PATH += ";C:\Temp" # Load the IEDriverServer.exe from a path where it is stored
$ie_object = New-Object "OpenQA.Selenium.IE.InternetExplorerDriver" # Instantiate Internet Explorer

# Using Element ID for this example, but I prefer using The Name element.
$ie_object.Navigate().GoToURL( "https://website.com/login") # Navigate to login page URL
$InputUser = $ie_object.FindElementById("ysi_email") # Find the ID element for username input textbox, gt this from the DOM developer Tools search
$InputUser.SendKeys("usernamegoeshere") # Push the text
$PasswordUser = $ie_object.FindElementById("ysi_password") # Find the ID element for password input textbox, get this from the DOM developer Tools search
$PasswordUser.SendKeys("passwordgoeshere") # Push the text
$LoginButton = $ie_object.FindElementById("ysi_btn_login") # Find the ID element for the submit button, get this from the DOM developer Tools search
$LoginButton.Click() #Sent click to the submit button

Get Selenium from: https://www.seleniumhq.org/download/

Thanks to newspaint, more info: https://newspaint.wordpress.com/2017/03/23/using-powershell-2-0-with-selenium-to-automate-internet-explorer-firefox-and-chrome/

like image 1
Maverick Sevmont Avatar answered Oct 19 '22 05:10

Maverick Sevmont


I am using PS 5.1 and Edge browser, and the above mentioned solution was not fully working as the $Link object did not contain Click Method. However, using the Submit() method did work. So basically in my case I had to change the next to last line to $Link.Submit()

like image 1
InfoSec Automation Avatar answered Oct 19 '22 07:10

InfoSec Automation