所以我在 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 Name:
</td>
<td>
<select name="ctl00$ContentPlaceHolder1$ddlAgentID" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$ddlAgentID\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_ddlAgentID">
<option selected="selected" value="">[Select...]</option>
<option value="BKR00372">1st National Bank</option>
<option value="BKR00012">21ST CENTURY INSURANCE & REINSURANCE BROKERS LIMITED</option>
<option value="BKR00613">21ST CENTURY INSURANCE & 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>
这是我遇到的错误。大约有 200 个下拉项目。我要找的东西在里面,我没有复制过来:
The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\Sabrin\Desktop\massy-test-web-prog.ps1:25 char:1
+ $ie.Document.getElementById("ctl00`$ContentPlaceHolder1`$ddlAgentID").value = "1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
感谢您的帮助!
最佳答案
我认为你的问题是时间问题。根据我在 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")
关于powershell 中的 html 对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35999907/