0

I have been using the following in a script with success:

$ie.document.getElementById("userid").value= "$user"

and

$comShell = New-Object -com "Wscript.Shell" 
$comShell.sendkeys($USERC)

I used the top one when the site has <input id="userid"> and if the id changes from site to site I just make minor alterations to the script. I have been using the second when the input is not defined with an id, but a name, as there seems to be no method of getElementByName. This only works with sites that by default have the focus set on the userid field.

I now encountered a site that defines the input with a name, not id, AND the user id field is not the default focus.

What could I use for something like this:

<INPUT SIZE="9" STYLE="font-family:Arial; font-size: 10pt; " type="text" name="USERID" maxlength="8" value="" onKeypress="passwordFocus(event)">

For my job I have been creating a utility to facilitate a number of tasks, one of which is a links area that provides shortcut (bookmarks) to some sites we frequently use and enters the generic id, so one does not have to try to find it at the last moment.

Any help would be greatly appreciated.

2
  • It seems odd to do this with PowerShell. Why not use the browser's integrated auto form completion and password manager? Commented Nov 9, 2014 at 16:54
  • I can't imagine why this was downvoted. Just because there's a different ("better") way of accomplishing this particular task, that doesn't mean it's not a worthwhile question to ask. The issue raised here is both more broadly applicable and quite tricky, so it's definitely useful Q&A material for this site. And I wish even a quarter of the questions from new users were this well-written. Commented Nov 9, 2014 at 23:23

1 Answer 1

1

Just as in JavaScript, there's no GetElementByName() method because multiple elements can have the same name; instead, there's a GetElementsByName() method, which returns a collection of all the elements with the given name. You need to use this method, and set the value property of the first (and presumably only) item in the collection.

Now, in JavaScript, it's as simple as

document.getElementsByName('USERID').value = 'myusername';

In PowerShell, however, accessing the items in the collection can be a little tricky. It's a COM collection, so you can't index into it with brackets, or you'll get an Unable to index into an object of type System.__ComObject error. Normally, you index into a COM collection using the Item() method with a 1-based index. So, you'd expect this to work:

$ie.Document.GetElementsByName('USERID').Item(1).value = $user

Instead, you get this error:

Method invocation failed because [System.__ComObject] doesn't contain a method named 'Item'.

WTF?? This collection doesn't have an index! D'oh!

It is, in fact, a collection nonetheless, I assure you of that. You can access the first item in two ways.

  1. Convert the collection to an array using the array expression evaluation operator @(). Then you can index into it using brackets and a 0-based index, as you normally would with a PowerShell array:

    @($ie.Document.getElementsByName('USERID'))[0].value = $user
    
  2. Pipe to Select-Object and select the first element:

    ($ie.Document.GetElementsByName('USERID') | select -First 1).value = $user
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.