4

So, I just started playing with Powershell today, and I've been searching, but I can't find the answer. I'm not sure if I'm even looking in the right place.

I need to use powershell to select the right combination in some dropdowns. One dropdown is a list of companies and the other is a list of services. The services list is populated depending on the company selection.

Using the IE ComObject:

$company = $doc.getElementById("id_companyselection")
$service = $doc.getElementById("id_companyservice")

foreach ($Item in $company) {
  if ($Item.text -eq "name_of_company") {
    $shippingMethod.value = $Item.value
  }
}

Is there a way to force Javascript to register the onchange event? By default the "service" dropdown, so I can't match the text that I have with the value that I need to select. Is there another way to do this that will use the existing value -> text mapping?

1
  • What do you mean with "the service dropdown"? Commented Apr 29, 2011 at 19:01

1 Answer 1

3

I ran into this at my last job, and never found a great solution. The problem is that when you change the .value you're just doing that on the DOM, so as you've found JavaScript doesn't notice the change.

The extremely hacky way I found to get this to work is to send keys to the element. I don't have the code in front of me so I can't tell you the exact syntax. This works because JavaScript sees this as the same as if the input came from the keyboard buffer, and will update accordingly.

This wouldn't be so bad if not for the following downside: The IE page has to be visible and active for send keys to work - you can't just send keys to the COM object. You make the COM object visible, then you select the process, then you can send keys to that window. This is obviously quite brittle and makes it so that the user can't multi-task while the script runs. I hope someone else has a more elegant method. :)

For your example of a drop-down rather than a text field, you'd probably have to select the field and then send down arrays to move to the different values of the drop-down.

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.