As for this...
But I don't need to open a new IE window, I need to use an existing IE
window, which is already open, before I start the ps script.
What / who opens this?
You have to discover what wind this is, in case of more that one IE session / tab is open.
You have to call the from object explicitly to act on the.
You also are using elements that don't really exist with what you are doing.
Since we cannot access you site, here is an example I provided a while back to someone have issues interacting with the aircanada site.
Now that site is a bite funky, as it is a mult-page site, with language popups recently added, I don't deal with the language popup in this example, so, just click past that manually, since I've not updated the code to deal with it. Once you click past that, that form will fill that values passed in.
You will note I am not using sendkeys for any of the page interactions. I am just using the from elements and passing in what values I want. Now this does open a new window for this due to the URL. Where as your code needs to look for that window first.
# Check from elements for the multi-page site
$url = 'https://www.aircanada.com/ca/en/ado/profile/sign-in.html'
($FormElements = Invoke-WebRequest -Uri $url -SessionVariable fe)
($FormElements = Invoke-WebRequest -Uri $url -SessionVariable fe).Forms
($Form0 = $FormElements.Forms[0]) | Format-List -Force
$Form0.Fields
($Form1 = $FormElements.Forms[1]) | Format-List -Force
$Form1.Fields
($Form2 = $FormElements.Forms[2]) | Format-List -Force
$Form2.Fields
$SiteSource = Invoke-WebRequest -Uri $url
$SiteSource.AllElements | Where{$_.TagName -eq "Button"} `
| Select-Object -Expand InnerText
$SiteSource.AllElements | Where{$_.TagName -eq "Button"} `
| Select-Object -Property * | Where innerText -eq 'SIGN IN'
So armed with the above, you get to do this.
# Navigate to aircanada
$url = 'https://www.aircanada.com/ca/en/ado/profile/sign-in.html'
# Instantiate IE
$ie = New-Object -com internetexplorer.application
$ie.visible = $true
$ie.navigate($url)
# Wait for IE to load
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1 }
# Take needed actions on the air canada site. Fill out the site page
($ie.document.getElementById('agencyIATA') | Select-Object -first 1).value = '1234567'
($ie.document.getElementById('agencyID ') | Select-Object -first 1).value = '789'
($ie.document.getElementById('bookingAgent') | Select-Object -first 1).value = '159'
($ie.document.getElementById('password') | Select-Object -first 1).value = '1234'
($ie.document.getElementById('rememberAgencyInfo') | Select-Object -first 1).value = $true
# Start-Sleep -Seconds 2
# ($ie.Document.IHTMLDocument3_getElementsByTagName('button') `
# | Where-Object innerText -eq 'SIGN IN').Click()
All this being said, see also:
Controlling Internet Explorer object from PowerShell
https://blogs.msdn.microsoft.com/powershell/2006/09/10/controlling-internet-explorer-object-from-powershell