Manipulating javascript dropdown with powershell

So, I just started playing with Powershell today, and I searched, but I can not find the answer. I'm not sure that I even look at the right place.

I need to use powershell to select the right combination in some dropdown menus. One drop-down list is a list of companies, and the other is a list of services. The list of services is filled out depending on the choice of company.

Using 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 get Javascript to log onchange event? The default drop-down list is "service", so I cannot match the text that I have with the value I need to select. Is there any other way to do this that will use the existing value -> text display?

+4
source share
1 answer

I came across this at my last job and never found a great solution. The problem is that when the value changes. You just do it on the DOM, since you found JavaScript, it does not notice the change.

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

This is not so bad if it weren’t for the following drawback: the IE page must be visible and active to send keys for work - you cannot just send keys to a COM object. You make the COM object visible, then you select the process, then you can send keys to this window. This is obviously quite fragile and makes it so that the user cannot multitask while the script is running. Hope someone has an even more elegant method. :)

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

+3
source

All Articles