In this part we will be handling automated logins on web portals.
I’ve setup a small web page with a login form at https://testzone.fl0re.com/loginportal/index.html so we can test our script.

(Portal Code source: https://www.formget.com/javascript-login-form/ )

The credentials are

Usernamestandard_user
PasswordSecretPassw0rd

Once again, let’s start with setting up our base script.

Setting up the base script.

$objInternetExplorer = New-Object -ComObject InternetExplorer.Application;
$objInternetExplorer.visible = $true;
$objInternetExplorer.navigate("https://testzone.fl0re.com/loginportal/index.html");

#Make sure the page is ready.
while($objInternetExplorer.ReadyState -ne 4)
{
    Start-Sleep -Milliseconds 100;
}

This should open Internet Explorer and go to the portal.

We will be using the techniques from part 2 to fill in the username and password fields, and finally click the button. So all of this should be pretty self explanatory.

Filling in the fields

Once again, right click Username field, and select Inspect Element.
You should see the following HTML code highlighted:

<input name="username" id="username" type="text">

And for the password field:

<input name="password" id="password" type="password">

And finally for the login button:

<input class="btn-validate" onclick="validate()" type="button" value="Login">

I didn’t add the ID for the button, so we will have to work around this. Let’s start with filling in the username and password fields first.

Just like when ‘clicking’ a button using the .Click() method, we can assign values to fields using the .value property.

$htmlelementUsername = $objInternetExplorer.Document.IHTMLDocument3_getElementById("username");
$htmlelementUsername.value = "standard_user"

If you want to find out all methods / properties for an element, you pipe it to the Get-Member cmdlet.

$htmlelementUsername | Get-Member

Next, do the same for the password field:

$htmlelementPassword = $objInternetExplorer.Document.IHTMLDocument3_getElementById("password");
$htmleementPassword.value = "SecretPassw0rd"

If you run the script now, you should see both fields populated:

Now all we need to do is click the button. Our COM object has 3 methods available to get elements: by ID, Name or TagName.

The only usable method we can use is IHTMLDocument3_getElementByTagName(). So lets use this with the “input” tag and check our results

$htmlelementButton = $objInternetExplorer.Document.IHTMLDocument3_getElementsByTagName("input");
$htmlelementButton | FT

We can see the following output:

From here we can use the className property to get our button element, using the Where-Object cmdlet in combination with the Select-Object cmdlet. When we inspected the button element, we saw that the button had a className of “btn-validate”.

We use the Where-Object to get all the elements from our selection where the className is equal to “btn-validate”. In our case it’s only one. However it will still return an array, so we will use Select-Object -First 1 to get the first object from the returned array. Once we get that object, we simply click it.

$htmlelementButton = $objInternetExplorer.Document.IHTMLDocument3_getElementsByTagName("input") | Where-Object {($_.className -eq "btn-validate")} | Select-Object -First 1;
$htmlelementButton.Click();

And this should log us in successfully.

Full Script

$objInternetExplorer = New-Object -ComObject InternetExplorer.Application;
$objInternetExplorer.visible = $true;
$objInternetExplorer.navigate("https://testzone.fl0re.com/loginportal/index.html");

#Make sure the page is ready.
while($objInternetExplorer.ReadyState -ne 4)
{
    Start-Sleep -Milliseconds 100;
}

#Fill in the username
$htmlelementUsername = $objInternetExplorer.Document.IHTMLDocument3_getElementById("username");
$htmlelementUsername.value = "standard_user"

#Fill in the password
$htmlelementPassword = $objInternetExplorer.Document.IHTMLDocument3_getElementById("password");
$htmlelementPassword.value = "SecretPassw0rd"

#Get the button, first by getting all objects with the tag "input", and finally getting the first item with a classname "btn-validate".
$htmlelementButton = $objInternetExplorer.Document.IHTMLDocument3_getElementsByTagName("input") | Where-Object {($_.className -eq "btn-validate")} | Select-Object -First 1;
$htmlelementButton.Click();

Leave a Reply

Your email address will not be published. Required fields are marked *