PowerShell - IE对象,设置值时出错

时间:2015-02-06 12:29:20

标签: internet-explorer powershell

我有一个简单的问题和答案webfrom,其来源如下:

  <label for="question1" data-role="question-label">
    1. What&#39;s going well? Any wins (big or little) this week?
</label>
<div class="row answer-list">
    <div class="col-xs-12 col-sm-12">



        <div class="row" data-role="answer-box-item">
            <div class="col-xs-12 col-sm-9" data-role="answer-box-content-wrapper">
                <span class="edit-textile-notifications">
                    <span class="glyphicon glyphicon-ok ok-status"></span>
                    <span class="not-sent-answer hidden" data-style="zoom-out">not sent</span>
                </span>
                <p type="text" class="edit-textile form-control input-sm answer-box-content" placeholder="create new answer" data-answer="true" data-question-id="1" data-role="new-answer-form"></p>

            </div>
            <div class="col-xs-12 col-sm-3 answer-actions-wrapper" style="display: none;">
                <div class="btn-group btn-group-sm answer-actions" >
                    <button type="button" class="btn btn-default answer-delete ladda-button" title="remove answer" data-style="zoom-out">
                        <span class="ladda-label">
                            <span class="glyphicon glyphicon-remove"></span>
                        </span>
                    </button>

我正在尝试使用PowerShell填充此webform。但我的代码不起作用

cls
$ie = new-object -com "InternetExplorer.Application"
Start-Sleep -Seconds 1
$ie.Navigate("https://test.contoso.internal")
Start-Sleep -Seconds 1
$doc = $ie.Document
$questions = $doc.body.getElementsByClassName("edit-textile form-control input-sm answer-box-content")
foreach ($questions in $questions) {
if ($question -like "data-question-id="1"") {$question.value = "Answer 1"}
if ($question -like "data-question-id="2"") {$question.value = "Answer 2"}}
你可以看看,看看我做错了什么,并纠正我的代码。我不是一个高端的强力人物:(

1 个答案:

答案 0 :(得分:0)

在尝试获取元素以确保数据已完全加载(在$ ie.Navigate下方)之前,您应该添加此行:

while($ie.ReadyState -ne 4) {start-sleep -m 100}