0

I'm trying to change the dropdown selection values of a webpage using PowerShell but had no luck.

The HTML code:

<div class="col-xs-10 col-sm-9 col-md-6 col-lg-5 form-field input_controls">
    <div ng-non-bindable="" class="hidden">
        <input type="hidden" id="sys_original.sc_task.state" name="sys_original.sc_task.state" value="2">
    </div>
    <select aria-required="false" aria-labelledby="label.sc_task.state" ng-non-bindable="true" name="sc_task.state" id="sc_task.state" onchange="onChange('sc_task.state');" style="; " class="form-control  ">
    <option value="" role="option" disabled="">-- None --</option>
    <option value="4" role="option" disabled="">Closed Incomplete</option>
    <option value="-5" role="option">Pending</option>
    <option value="-1" role="option" disabled="">Queued</option>
    <option value="1" role="option" disabled="">Open</option>
    <option value="2" selected="SELECTED" role="option">In Progress</option>
    <option value="7" role="option">Cancelled</option>
    <option value="3" role="option">Closed Complete</option>
    </select>
</div>

I tried these in PowerShell:

$IE = New-Object -ComObject 'internetExplorer.Application'
$IE.Visible= $TRUE


$StatusField = $IE.Document.IHTMLDocument3_getElementByID("sc_task.state")
$StatusField.value = '-5'
$StatusField.fireEvent("onchange")
($StatusField | where {$_.innerHTML -eq "-5"}).Selected = $true

And it gives me the below errors:

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At C:\...ps1:53 char:1
+ $StatusField.value = '-5'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\...ps1:54 char:1
+ $StatusField.fireEvent("onchange")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The property 'selected' cannot be found on this object. Verify that the property exists and can be set.
At C:\....ps1:55 char:1
+ ($StatusField | where {$_.innerHTML -eq "-5"}).Selected = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

Please help.

2
  • You have not posted how $IE is defined. That seems to be a critical piece of information. Commented Oct 17, 2019 at 11:53
  • $IE = New-Object -ComObject 'internetExplorer.Application' $IE.Visible= $TRUE I have been able to login into the website and modify some fields without any issue. I'm just stuck with this selection field. Commented Oct 17, 2019 at 11:57

2 Answers 2

1

You could refer to the following code to use the querySelector and JQuery selectors to find the special option and select it.

Edit

If the select element is inside the iframe tag, we should first find the iframe, then, using the contentDocument and querySelector method to find the select element, like this:

$ie = New-Object -ComObject internetexplorer.application
$ie.Visible = $true
$ie.Navigate("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange")
while ($ie.Busy) {Start-Sleep -Milliseconds 500}
$selectedvalue = "Volvo"
$ie.Document.getElementById("<iframe id attribute>").contentDocument.querySelector("#mySelect option[value=$selectedvalue]").selected = $true
$ie.Document.getElementById("<iframe id attribute>").contentDocument.getElementById("mySelect").FireEvent("onchange")

If not using Iframe control, you could use the following code:

$ie = New-Object -ComObject internetexplorer.application
$ie.Visible = $true
$ie.Navigate("<the website url>")
while ($ie.Busy) {Start-Sleep -Milliseconds 500}
$selectedvalue = "1"
$ie.Document.getElementById("sc_task.state").querySelector("option[value='$selectedvalue']").selected = $true
$ie.Document.getElementById("sc_task.state").FireEvent("onchange")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion @zhi-lv-MSFT but as the selection field was inside an iFrame this didn't help. I have shared the working code here.
hi kaz, I have modified the code, it works well with your html elements, please check above code.
0

I found out that the dowpdown selection field was inside an iFrame due to which IHTMLDocument3_getElementByID was not working. I made the following change to my code and it is working fine now:

$StatusField = $IE.Document.IHTMLDocument3_getElementByID('gsft_main').contentWindow.document.IHTMLDocument3_getElementByID('sc_task.state')
$StatusField.value = '-5'

1 Comment

Glad to hear you have solved the problem, congratulation.

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.