0

I am working on a powershell script, which should fill an IE form, but it doesn't send the value to the form, instead, it just writes the value ("search text") into the Powershell script..?

I have copied my code from this post: Filling web form via PowerShell does not recognize the values entered

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.
My code (for your info,"lst-ib" is the id of google.at's search field):

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")

$app = new-object -com shell.application
$ie = $app.windows() | where {$_.Type -eq "HTML Document"}
$ie.visible = $true;

while($ie.Busy) { Start-Sleep -Milliseconds 100 }

$ie.document.IHTMLDocument3_getElementById("lst-ib").focus()
Start-Sleep -Milliseconds 1000; 
[System.Windows.Forms.SendKeys]::Sendwait("test searchtext");   
coor_angar
  • 93
  • 7
  • I don't know the reason for your problem, but it seems to be an issue, that your script has the focus (even if you use the focus() function). If you move the `Start-Sleep` to before the `ie.document` and start the script again and click into your IE Window, it will start to fill.. Maybe it helps with troubleshooting... – Dan Stef Aug 10 '18 at 13:13
  • Have a look here https://blogs.technet.microsoft.com/heyscriptingguy/2011/01/10/provide-input-to-applications-with-powershell/ . You probably need to add `[Microsoft.VisualBasic.Interaction]::AppActivate(process-id)` – Dan Stef Aug 10 '18 at 13:42

1 Answers1

0

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

postanote
  • 15,138
  • 2
  • 14
  • 25