简体   繁体   中英

Powershell inputting data into a website - property value cannot be found on a GetElementByName

I'm using the below code to interact with a html website at work, The first part to log in works however when i try and run the part

($ie.document.getElementsByName("textSearch") | select -first 1).value = "12345";

I get the following error

The property 'value' cannot be found on this object. Verify that the property exists and can be set.

At C:\\Users\\lawsod03\\desktop\\powershell\\DCS-remove.ps1:23 char:1

+ ($ie.document.getElementsByName("businessArea") |select -first 1).value = "MCS_C ...

+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (:) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

This is the input field I am currently trying to add a value to

<td class="td_prompt">
    <input name="textSearch" value="">&nbsp;by
</td>

The PowerShell script is below:

cls
$username = "user";
$password = "pass";
$loginUrl = "www.website.com/login";
$MainCSA = "www.website.com/usersearch";
$iterator = 1;

#initialize browser
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($loginUrl);
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; } #wait for browser idle

#login
($ie.document.getElementsByName("username") |select -first 1).value = $username;
($ie.document.getElementsByName("password") |select -first 1).value = $password;
($ie.document.getElementsByName("submit") |select -first 1).click();
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }    #wait for browser idle

#navigate to maintain CSA
$ie.navigate($MainCSA);
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }    #wait for browser idle
($ie.document.getElementsByName("textSearch") |select -first 1).value = "12345";

I've searched online for a solution however i can't seem to find one that has pointed me in the right direction.

Any help is much appreciated.

Instead of using select -f 1 change your format to do a coerced de-reference like this ( I've also simplified your code ):

function getEl ($ie, $name) {return @($ie.document.getElementsByName($name))[0]}
(getEl $ie 'username').value = $username
(getEl $ie 'password').value = $password
(getEl $ie 'submit').click()

This is because getElementsByName() returns a list of elements ( though not in a traditional array, thus the @() cast converts it to PowerShell array, and then you can de-reference like a normal array ).

Does that help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM