【发布时间】:2015-03-21 04:42:13
【问题描述】:
我正在编写一个登录脚本,它将位于后台并将串行端口数据传递到网页。 除了最后一次点击,我已经完成了所有工作。基于这两个帖子:
Powershell click on javascript link Execute javascript trough Internet Explorer's com interface using PowerShell
这是我的脚本:
# Variables
$username = "user"
$password = "pass"
$item = ""
$portnumber = "COM6"
#open port and wait for data
$port = new-object system.io.ports.serialport $portnumber,9600,none,8,one
$port | add-member -membertype "NoteProperty" -name "identifier" -value "Serial"
$port.open()
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
Function Login($name,$pass){
$ie.navigate2("http://site/login.aspx")
while($ie.busy) {start-sleep -s 1}
$ie.document.getElementById("LoginName").value= $name
sleep -s 1
$ie.document.getElementById("Password").value = $pass
sleep -s 1
$ie.document.getElementById("LoginBtn").click()
sleep -s 2
}
Function Lookup($itemnumber){
$ie.navigate2("http://site/itemLookup.aspx")
while($ie.ReadyState -ne 4) {start-sleep -s 1}
$ie.document.getElementById("ctl00_PageBody_ItemNumberBox").value = $itemnumber
sleep -s 1
$ie.document.getElementById("ctl00_PageBody_RetrieveButton").click()
while($ie.ReadyState -ne 4) {start-sleep -s 1}
sleep -s 2
$details = @( $ie.document.getelementsbytagname('a')) | where-object {$_.innerText -eq 'Details'}
$details.click()
}
try{
Do{
$itemno = $port.readline()
write-host $itemno
#check for ie not timed-out
if (!($ie.navigate2("http://site/dashboard.aspx"))) {
sleep -s 2
Login -name $username -pass $password
Lookup -itemnumber $itemno
}
else { Lookup -itemnumber $itemno }
}
while ($port.isopen -eq $true)
}#end try
catch{"$_" ; $ie.quit(); $port.close(); $port.dispose(); exit}
在查找功能中是我遇到问题的地方。我可以在控制台中运行每一行,它工作正常,但作为一个脚本,它在分配 $details 变量时失败,因此不会加载最后一页。
这是我尝试与之交互的页面元素:
<tr>
<td colspan="4">
<p id="ctl00_PageBody_ItemInfoPara" class="tabletext">This item's status is Normal. [<a href="javascript://" onclick='ViewItemInformation(11770856,false);'">Details</a>]</p>
</tr>
<tr>
<td colspan="4">
这与范围有关吗?还是我只是错误地与页面交互? 任何帮助将不胜感激。
【问题讨论】:
-
我能够通过将这两个函数折叠到一个 Main 函数中来完成这项工作,因此这似乎是一个范围问题。关于如何解决这个问题的任何想法?
标签: javascript powershell serial-port