Trying to automate IE

I am working on a script to automate filling out a ticketing system in IE. Typically for web browser automation I turn to AutoIt, but thought I would try if I could do this purely through PowerShell. Creating the page and navigating to it works just fine:

$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$oIE = New-Object -ComObject "InternetExplorer.Application" -ErrorAction Stop
$oIE.width = $screen.width
$oIE.height = $screen.height
$oIE.Top = 0
$oIE.Left = 0
$oIE.Visible = $true
$oIE.Silent = $true
    Start-Sleep -Milliseconds 1000; 

I am unable, however, to pull the fields from the form. I tried something like this to get the main form:

$oCol = $oIE.document.forms.item("mainForm")

but no joy (I tried in AutoIt, and it works fine, so not sure why I can’t in PowerShell). Also tried just getting the element by ID, like so (using a description field as example):

$oIE.document.getElementsByTagName("input") | % {
    if ($ -ne $null){
        if ($"Title4")) {
            "Found it"

Based on the html line:

input type=‘text’ id=‘mainForm-Title4’ name=‘Title’ value=‘’

But I get “You cannot call a method on a null expression” on the getElementsByTagName call.

Even something so simple as trying to get the $oIE.Document doesn’t work. Just hoping someone can point out what I am obviously doing wrong.

In my tests, I was able to find specific forms with these:

$oIE.document.getElementsByTagName("input") | where { $ -eq 'companyNbr' }

Or if you don’t know the exact ID, you can use -match:

$oIE.document.getElementsByTagName("input") | where { $ -match 'companyNbr' }


I should’ve read through to the last two lines, sorry!

After running

$oIE = New-Object -ComObject "InternetExplorer.Application" -ErrorAction Stop
$oIE.Visible = $true
you should see an Internet Explorer window pop up on your main screen. If you aren't seeing that then, for whatever reason, the object creation likely failed. If you've run it multiple times, it might help to try killing all iexplore.exe processes and try running the code again. If that is causing a mix-up, try running $oIE.Quit() before running the code to create the object again.

I do see the object created, the window pops up. My problem has been getting any of the objects in the DOM. Based on the viewer, and the one field having an id of id=‘mainForm-Title4’, I should be able to do something like

$oIE.document.getElementsByTagName(“input”) | where {$ -match ‘Title4’}

but still get the same error about it being a null expression