使用PowerShell更改下拉选项

时间:2019-10-17 11:13:20

标签: powershell internet-explorer web-scraping

我正在尝试使用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 个答案:

答案 0 :(得分:1)

您可以参考以下代码来使用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")

答案 1 :(得分:0)

我发现dowpdown选择字段位于iFrame内,这是因为IHTMLDocument3_getElementByID无法正常工作。我对代码进行了以下更改,现在可以正常运行:

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