powershell中的html对象错误

powershell中的html对象错误,html,internet-explorer,powershell,Html,Internet Explorer,Powershell,所以我在powershell中做了一点IE自动化 这是我的密码: $text = "ctl00`$ContentPlaceHolder1`$Login1`$UserName" $ie = New-Object -ComObject internetexplorer.application $ie.visible = $true $ie.navigate($url); while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 1000; }

所以我在powershell中做了一点IE自动化

这是我的密码:

$text = "ctl00`$ContentPlaceHolder1`$Login1`$UserName"
$ie = New-Object -ComObject internetexplorer.application
$ie.visible = $true
$ie.navigate($url);
while ($ie.Busy -eq $true)
{

Start-Sleep -Milliseconds 1000;
} 

Write-Host -ForegroundColor Green "Attemptin to login to website."
$ie.Document.getElementById("ctl00`$ContentPlaceHolder1`$Login1`$UserName").value = $username
$ie.Document.getElementById("ctl00`$ContentPlaceHolder1`$Login1`$Password").value = $password
$ie.Document.getElementById("ctl00`$ContentPlaceHolder1`$Login1`$LoginButton").Click()

#while ($ie.Busy -eq $true) 
#{ 
Start-Sleep -Milliseconds 5000; 
#}
$ie.Document.getElementById("ctl00`$ContentPlaceHolder1`$txtAgentID").value = "BKR00822"
我遇到的问题是这一行:

$ie.Document.getElementById("ctl00`$ContentPlaceHolder1`$ddlAgentID").value = "BKR00822"
这一行是指一个下拉列表

出于某种原因,它不接受任何属性、值或选择

以下是html的来源:

        <div id="ctl00_ContentPlaceHolder1_pnlOverrideAgentIDEnter">

            <div class="override_agent_text_container">
                Please override the Agent ID below to view policies of another agent.
            </div>
            <table cellpadding="0" cellspacing="0" border="0" class="override_agent_controls_container">
                <tr>
                    <td width="10px">Agent&nbsp;Name:&nbsp;
                    </td>
                    <td>
                        <select name="ctl00$ContentPlaceHolder1$ddlAgentID" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$ContentPlaceHolder1$ddlAgentID\&#39;,\&#39;\&#39;)&#39;, 0)" id="ctl00_ContentPlaceHolder1_ddlAgentID">
                        <option selected="selected" value="">[Select...]</option>
                        <option value="BKR00372">1st National Bank</option>
                        <option value="BKR00012">21ST CENTURY INSURANCE &amp; REINSURANCE BROKERS LIMITED</option>
                        <option value="BKR00613">21ST CENTURY INSURANCE &amp; REINSURANCE BROKERS LIMITED</option>
                        <option value="BKR00824">345-815-2126</option>
                        <option value="BKR00022">ACE INSURANCE BROKERS LIMITED</option>
                        <option value="BKR00783">Agostini Insurance Brokers (St. Lucia) Ltd</option>
                        <option value="BKR00032">AGOSTINI INSURANCE BROKERS LIMITED</option>
                        <option value="BKR00623">AGOSTINI INSURANCE BROKERS LIMITED</option>
                        <option value="BKR00633">AGOSTINI INSURANCE BROKERS LIMITED</option>
                        <option value="BKR00042">AMALGAMATED INSURANCE BROKERS LIMITED</option>
                        <option value="BKR00643">AMALGAMATED INSURANCE BROKERS LIMITED</option>


谢谢你的帮助

我认为你的问题在于时机。我在IE自动化方面有很多这样的经验

在等待IE页面加载时,这段代码对我最为有效:

While ($ie.ReadyState -ne 4)
{
    write-host "Session State: $($ie.ReadyState)"
    Start-Sleep -Milliseconds 100
}
Start-Sleep -Milliseconds 200 # extra buffer 
$ie.Busy
不可靠

而且

要在下拉列表中选择项目,您可能应该使用
selectedIndex
属性:

$ie.Document.getElementById("list").selectedIndex = 69 
$ie.Document.getElementById("list").FireEvent("onchange")   

不,仍然会得到相同的错误。在对象上找不到属性只是出于好奇,您是否得到找不到的属性“selectedIndex”?只需重新读取您的html。。。您要查找的实际ID是“ctl00\u contentplaceholder 1\u ddlAgentID”。您现在拥有的是对象名,而不是ID。
$ie.Document.getElementById("list").selectedIndex = 69 
$ie.Document.getElementById("list").FireEvent("onchange")