user1735120
user1735120

Reputation: 489

PowerShell fill up form and submit

I need to fill up a form of an internal site. I was able to successfully login to the portal page using this code

$ie = New-Object -Com InternetExplorer.Application 
$ie.Visible = $true
$ie.Navigate("https://some-internal-ip/login") 
while ($ie.ReadyState -ne 4) {Start-Sleep -m 100};

#Login 
$form = $ie.document.forms[0]
$inputs = $form.GetElementsByTagName("input")
($inputs | where {$_.Name -eq "username"}).Value = $username
($inputs | where {$_.Name -eq "password"}).Value = $password
($inputs | where {$_.Name -eq "action:Login"}).Click()

#after login - navigate to this link
while ($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
Start-Sleep -m 2000
$ie.Navigate("https://some-internal-ip/monitor/users")

Above works fine. It'll direct me to the new link where I need to fill another form. So I'm reusing my code above to fill up a form for a user field and submit button.

There are multiple forms inside this page so I want to narrow it down to this specific id="trackingSearchForm".

HTML form:

<form action="https://.../" method="POST" name="trackingSearchForm" id="trackingSearchForm" accept-charset="utf-8" onsubmit="return false;">
<input name="user" onkeypress="keyPressHandler(this.form, event)" class="" type="text" id="user" value=">
<input type="button" class="submit" id="submitButton" value="Search" onclick="performSearch();">

Get ID of form and fill up

while ($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
$form =  $ie.Document.Forms[0]
$form = ($ie.Document.Forms[0] | where {$_.Id -eq "trackingSearchForm"})
$inputs = $form.GetElementsByTagName("input")
($inputs | where {$_.name -eq "user"}).Value = "john"
($inputs | where {$_.Id -eq "submitButton"}).Click()

but I'm getting these errors:

You cannot call a method on a null-valued expression.
+ $inputs = $form.GetElementsByTagName("input")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The property 'value' cannot be found on this object. Verify that the property
exists and can be set.
+ ($inputs | where {$_.Name -eq "user"}).Value = "john"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The same code I used for the login form doesn't work on another form.

Upvotes: 1

Views: 7185

Answers (1)

Joost
Joost

Reputation: 1216

The error is probably in this line:

$form = ($ie.document.forms[0] | where {$_.id -eq "trackingSearchForm"})

Remove the index of $forms:

$form = ($ie.document.forms | where {$_.id -eq "trackingSearchForm"})

Upvotes: 1

Related Questions