Изменить раскрывающийся список с помощью PowerShell - PullRequest
0 голосов
/ 17 октября 2019

Я пытаюсь изменить раскрывающиеся значения выбора веб-страницы с помощью PowerShell, но безуспешно.

HTML-код:

<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>

Я пробовал их в 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

И это дает мне следующие ошибки:

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

Пожалуйста, помогите.

Ответы [ 2 ]

1 голос
/ 17 октября 2019

Вы можете обратиться к следующему коду, чтобы использовать селекторы querySelector и JQuery, чтобы найти специальную опцию и выбрать ее.

Редактировать

Если элемент select равенвнутри тега iframe сначала нужно найти iframe, а затем, используя методы contentDocument и querySelector, найти элемент select, например:

$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")

Если элемент управления Iframe не используется, вы можете использовать следующий код:

$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")
0 голосов
/ 18 октября 2019

Я обнаружил, что поле выбора dowpdown было внутри iFrame, из-за которого IHTMLDocument3_getElementByID не работал. Я внес следующие изменения в свой код, и теперь он работает нормально:

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