1

I encountered a problem while trying automated login on a website using PowerShell IE Com Object. My script could pass text values into both email and password forms, but they seem unrecognised by the site. As such, the login button is unclickable, preventing login process. There are prompts to provide credentials despite the script having done so as shown below:

Forms input passed by script not recognised

When I edit the texts in the forms directly from the IE window (added random characters), the texts became recognised and the login button would then become lit and clickable. As the credentials I used are random, the site returned an invalid message.

Forms input recognised after direct edit

Invalid login message

May I request for help on why the text are not recognised despite the script having passed them into the forms. Any advice or workaround is very much appreciated.

The website is https://juice-shop.herokuapp.com/#/login

My script is as shown below:

$url = "https://juice-shop.herokuapp.com/#/login"
$email = "[email protected]" #random email
$password = "password" #random password

$ie = New-Object -ComObject 'InternetExplorer.Application'
$ie.Visible = $true
$ie.Navigate($url)

#Wait for browser to finish
while($ie.Busy){Start-Sleep -Seconds 2}

#email form input
$emailForm = $ie.Document.IHTMLDocument3_getElementById('email')
$emailForm.click()
$emailForm.value = $email

#password form input
$passwordForm = $ie.Document.IHTMLDocument3_getElementById('password')
$passwordForm.click()
$passwordForm.value = $password

#click login button
$submitButton = $ie.Document.IHTMLDocument3_getElementById('loginButton')
$submitButton.click()

1 Answer 1

0

The problem is that click() method doesn't trigger an input event. There's probably a simpler way to do this, but this code will dispatch the input events for email and password, then the click event for the loginButton:

$ie = New-Object -ComObject 'InternetExplorer.Application'
$ie.Visible = $true
$ie.Navigate("https://juice-shop.herokuapp.com/#/login")
do {Start-Sleep 1}while($ie.Busy) #wait a second for IE to get busy

$ie.Document.getElementById('email').value = '[email protected]'
$ie.Document.getElementById('password').value = 'test123'

$script = @"
 var evt= document.createEvent('HTMLEvents');
 evt.initEvent('input',true,true);
 var submitEvt= document.createEvent('HTMLEvents');
 submitEvt.initEvent('click',true,true);
 document.getElementById('email').dispatchEvent(evt);
 document.getElementById('password').dispatchEvent(evt);
 document.getElementById('loginButton').dispatchEvent(submitEvt);
"@
$ie.Document.parentWindow.ExecScript($script)
Sign up to request clarification or add additional context in comments.

4 Comments

Ohh i see, the triggering of input and click events make sense. I ran the script above but was met with an error which is "cannot call a method on a null-valued expression". Tried outputting $ie.Document.parentWindow which shows null. Any idea why? Much thanks!
Nice job debugging! I think you're on the right track, trying to figure out why the parentWindow is null. I'd guess that we're on different versions of Internet Explorer that expose a different COM interface. I looked up how to execute javascript in Internet Explorer via Powershell automation, but that may have only been accurate for Windows 10 and IE 11. Maybe there's a different interface for your version of Windows or IE. When I execute this script it shows "Invalid email or password." above the email box. Does it show the same for you?
This question mentions an IE protected mode, which may be part of your problem.
Ahh so the missing 'Microsoft.mshtml.dll' from GAC folder was the issue. I updated my system to latest Windows 10 but it still didn't work until I followed the link you sent and copied the folder/file structure into my system. The script is now working for me, text is recognised and button is clickable. "Invalid email or password" also appears above the email box. Thank you very much for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.