using powershell to click a link on a web page and then pass credentials

automated-tests, html, powershell

Solution

Write-Output "- Kill all IE windows"
get-process iexplore | stop-process -Force
Start-Sleep -s 1

function that open IE for a given url and a password retrieved from encrypted file

function OpenIE([string]$url, [string]$p)
{

Open Internet Explorer with a given url

$wshell = New-Object -com WScript.Shell
$wshell.Run("iexplore.exe $url")
Start-Sleep 1

Send user credentials to IE

$wshell.sendkeys("+{TAB}")
$wshell.sendkeys("username")
$wshell.sendkeys("{TAB}")
$wshell.sendkeys($p)
$wshell.sendkeys("{ENTER}")
}

$credentialsfile = "C:\temp\credfile.txt" 

Check if credential file exists

if (Test-Path $credentialsfile)
{  

Retrieve the credentials from an encrypted file

$encp = get-content $credentialsfile | convertto-securestring 
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($encp) 
$p = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) 
remove-variable encp,ptr 
}else{

Request credentials and save to encrypted file

$creds = Get-Credential –credential DOMAIN\user
$encp = $creds.password 
$encp |ConvertFrom-SecureString |Set-Content $credentialsfile
}

Write-Output "- open IE"
OpenIE "http://www.yoururlhere.com" $p

Problem

I am trying to test our a web page we built that has a temp ssl cert, the issue is that when I goto the page I get the IE security warning that the ssl cert is invalid, with two links one to close the page, and the other to proceed to the page. I was able to use powershell to open IE and click the link on the ssl warning page, but now I need to populate the username and password input boxes and then click the login button. ``` $url = "res://ieframe.dll/invalidcert.htm?SSLError=50331648#https://10.2.2.1:8050/showme.do" $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 "j_username" }.value = "user" $ie.Document.getElementsByName("input") | where { $.Name -eq "j_password" }.value = "password" $loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'LoginButton'} $loginBtn.click() ``` So right now the page opens but the input fields are not populated or the button clicked, Do I need some kind of loop or while statement? thanks

Original source