Search code examples
formspowershellclicktoken

Form Click() with a Token


Here is what I am trying to do:

  1. Open a site and click "I accept" button to accept terms. (works)
  2. Above redirects me to a login page where I enter username and password. (works)
  3. Click "Log in". (fails, but I get no errors)

I have enabled Protected Mode, although I am unable to run scripts as Sys Admin. One other point is that, this site appears to post a token when clicking "Log in" button. Any idea why the solution does not work?

HTML:

<form id="mainform" method="post" autocomplete="off" action="/URL/" onsubmit="submitHandler(); return true;">
<table class="loginTable">

...

<tr>
  <td colspan="2" class="colSpanRow" id="loginSubmitTd">
    <input type="submit" value="Log in" tabindex="4" id="loginSubmit" />
  </td>
</tr>

PowerShell:

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

Start-Sleep -s 2

$link = @($ie.Document.GetElementsByTagName('A')) |
        Where-Object {$_.InnerText -eq 'I accept'}
$link.Click()

Start-Sleep -s 5

$ie.Document.IHTMLDocument3_getElementById('username').Value = "username"
$ie.Document.IHTMLDocument3_getElementById('password').Value = "pw"

$submitButton = @($ie.Document.IHTMLDocument3_getElementsByTagName('input')) |
                Where-Object {$_.Value -eq 'Log in'}
Write-Host $submitButton.id
$submitButton.Click()

Solution

  • I suggest you avoid using IHTMLDocument3_getElementsByTagName and use the getElementsByTagName function.

    In your code this would then be

    $submitButton = @($ie.Document.getElementsByTagName('input')) | Where-Object {$_.value -eq 'Log in'}
    

    or maybe even better:

    $submitButton = $ie.Document.getElementById('loginSubmit')