I'm writing a login script that will sit in the background and pass serial port data to a web page. I've gotten everything working but the last click. Based on these two posts:
Powershell click on javascript link Execute javascript trough Internet Explorer's com interface using PowerShell
This is my script:
# 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}
In the Lookup function is where I'm having trouble. I can run each line in a console and it works fine, but as a script it fails at assigning the $details variable, and therefore won't load the last page.
This is the page element I'm trying to interact with:
<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">
Is this something to do with scope? Or am I just interacting with the page incorrectly? Any help would be much appreciated.