Author Posts

August 31, 2016 at 9:02 pm

Hi Scripters! I'm working with Internet Explorer automation to launch and log on to two websites. I can successfully create the IE COM objects, pass them to a "login" function, and see that the site accepts the credentials. However, afterward I would like to periodically refresh the page in order to maintain the session. When I pass the existing IE objects to my "test-loginPage" function (designed to check if we're back at the login page, and either login again else refresh the current page), I consistently get the InvokeMethodOnNull error when trying to use the object's Refresh method. If I use the ISE debugger and put a breakpoint at the beginning of the test-loginPage function, I can verify that the object has been passed to the function, that it's still an IE object, and that Get-Member indicates it has a Refresh method. What am I doing wrong?

Here's the Gist; I'm running Windows 10.1511, so Powershell 5 interacting with IE 11.
Note: I did change the stored logon information so that it is not valid credentials, and therefore commented out the part of the test-loginPage function that would normally be checking if we're still on the login page. I still expect the code to fill in the login fields but I expect the site in question will fail the logon attempt.

Thanks in advance!

September 1, 2016 at 1:48 am

What's the full error message you receive?

September 1, 2016 at 1:09 pm

You cannot call a method on a null-valued expression.
At C:\Users\\Desktop\Watchdesk2.ps1:103 char:13
+ $ieObjToTest.Refresh()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Sorry, I forgot to include that the first time.

September 1, 2016 at 8:31 pm

I can't replicate the issue you're facing. The Refresh method works for me without error.

If you:
– comment out this line: login-website $startSite $ie
– and also keep the switch conditions in test-logonPage commented out as you had above,

does the fault still occur?

September 2, 2016 at 1:00 pm

Yes, same error. Also the same if I directly call the refresh method of the $ieTS/$ieTex objects instead of passing them to the test-logonPage function.

If I write out just the "launch website and refresh it" part, it works, as in:

$telestaff = "https://www.telestaff.net/servlet/ServletController?device=stdbrowser&action=doBeginLogin"
$ie = New-Object -com InternetExplorer.Application
$ie.Navigate($telestaff)
$ie.Visible = $true
$texcom = "https://www.texcom.com/login.php"
$ie2 = New-Object -com InternetExplorer.Application
$ie2.Navigate($texcom)
$ie2.Visible = $true
Start-Sleep -s 60
$ie.Refresh()
$ie2.Refresh()

Launches 2 IE windows, loads the appropriate pages, waits, and successfully refreshes both.

BUT, running only the "start-IE" function (logon still commented out) and then calling the refresh method on the returned object fails, even if I launch only a single page. So, if I pull just this out of the script and run it, I get the error:

$texcom = "https://www.texcom.com/login.php"

Function Start-IE($startSite) 
{
    $ie = New-Object -com InternetExplorer.Application
    $ie.Navigate($startSite)
    $ie.Visible = $true
    #wait for page to load
    while ($ie.Busy) 
    {
        start-sleep -s 1
    }
    #login-website $startSite $ie
    Return $ie
}

$ieTex = Start-IE $texcom
Start-Sleep -s 60
$ieTex.Refresh()

...Something about transporting the IE object out of the function? I still get what looks like the object if I ask for $ieTex after running the above code:
PS C:\Users\jflanagan> $ieTex

Application : System.__ComObject
Parent : System.__ComObject
Container :
Document : mshtml.HTMLDocumentClass
TopLevelContainer : True
Type : HTML Document
Left : 294
Top : 0
Width : 2511
Height : 1445
LocationName : Login | TexCom
LocationURL : https://www.texcom.com/login.php
Busy : False
Name : Internet Explorer
HWND : 199788
FullName : C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
Path : C:\Program Files (x86)\Internet Explorer\
Visible : True
StatusBar : True
StatusText :
ToolBar : 1
MenuBar : True
FullScreen : False
ReadyState : 4
Offline : False
Silent : False
RegisterAsBrowser : False
RegisterAsDropTarget : True
TheaterMode : False
AddressBar : True
Resizable : True

September 4, 2016 at 8:56 pm

I've build a Windows 10 test VM for this, JFlanagan, and now get the same issue.

I was using IE11 on Windows 8.1 and didn't get the error.

Will have a play around with it today.

  • This reply was modified 1 year, 11 months ago by  84rusty ..

September 4, 2016 at 9:21 pm

Ok,

So simply do this:

Change: $ieTex.Refresh()
to: $ieTex.Application.Refresh()

Tested this change works on a Windows 10 VM.

  • This reply was modified 1 year, 11 months ago by  84rusty ..

September 6, 2016 at 7:15 pm

Okay, that was pretty simple and it works for refreshing the page. Thanks 84rusty!

Can you point me at an explanation of when/why I would need to use the .Application part? I'd like to learn and so far I'm failing at searching for it.

September 6, 2016 at 8:20 pm

I can't find an explanation either 🙁

Maybe someone else can weigh in on this.