Filling web form via PowerShell does not recognize

2019-02-25 05:05发布

Working as a QA I need to fill in a lot of applications through a web form. Idea is to have the personal data in some xls/txt/whatever file, read the file and use Powershell to feed data to the browser.

When I use the code below to fill in the form in IE, even though it seems to work fine, I get an error when submitting the form that no data was entered.

Any ideas or suggestions how to get past this would be much appreciated

Sadly my resources are limited to Powershell 2.0. Selenium or any other "more sophisticated" tools are out of question at least for now.

validation error here

$ie = New-Object -com InternetExplorer.Application
$ie.Navigate("MyURL")
$ie.visible = $true

while ($ie.ReadyState -ne 4){sleep -m 100}

Function ClickById($id) {
    $ie.document.getElementById($id).Click()
}

### Základní údaje
$FnId = 'personalData.firstName'
$LnId = 'personalData.lastName'
$PhoneId = 'personalData.mobilePhone'
$EmailId = 'personalData.email'
$DataAgreementCheckBox = 'application.personalDataAgreement'
$SubmitfwdId = 'forward'


$Values = "Ublala", "Pung", "222333444", "ublala@pung.com"
$Ds1Elements = $FnId, $LnId, $PhoneId, $EmailId

$j = 0
foreach ($El in $Ds1Elements) {
    $ie.document.getElementById($El).value = $values[$j]
    $j++
}

ClickById $DataAgreementCheckBox
ClickById $SubmitfwdId

2条回答
我只想做你的唯一
2楼-- · 2019-02-25 05:31

Sometimes website forms will wait until the input box has lost focus to pre-validate the value before submitting the form. The text boxes may never get focus if you are manually setting the values in the background so the form believes that you haven't actually entered any values.

You may be able to get around this by manually focusing each text box in turn, and then focusing the submit button before clicking it. Each element in the $ie.Document should have a focus() method you can use.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-25 05:46

Thanks for the suggestion but it did not work as the form seemes to be stupid in many ways. Anyway I used your advice for the focus when going with SendKeys method and it did the trick.

At the beginning I needed to load this assembly

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")

and then changed the loop accordingly to use the SendKeys method

$j = 0
foreach ($El in $Ds1Elements) {
    $ie.document.getElementById($El).focus()
    [System.Windows.Forms.SendKeys]::Sendwait($values[$j]);   
    $j++
}

And tradaaaa the form is filled and nobody is complaining :)

查看更多
登录 后发表回答