Author Posts

September 8, 2015 at 3:52 pm

Hi all,
Here is my predicament, when I run the last line in the command pane, it works but when I run the whole code it gives the ".....null-valued expression" error.
Here is my code:
# Initiate an internet explorer object
$ie = New-Object -com InternetExplorer.Application
$ie.Visible = $True
$url = ""
while($ie.busy){Start-Sleep 1}

# Enter login information
$ie.Document.getElementById("js-username").value = "username"
$ie.Document.getElementById("password_field").value = "password"
while($ie.busy){Start-Sleep 1}

# Click Inventory Management under Inventory & EDI
$link = $ie.Document.getElementsByTagName('a') | Where-Object {$_.innerText -eq 'Inventory Management'}
while($ie.busy){Start-Sleep 1}
# Click on Import Inventory Data as button to upload excel file, then upload file
while($ie.busy){Start-Sleep -s 1}
# Last line which gives the error

Any ideas what I might be doing wrong? Last line clicks a button on a modal box.
I ran the code separately and works perfect.

September 9, 2015 at 4:33 am

You might want to look at $ie.ReadyState which indicates if the page load is complete. If memory serves, 4 is complete. Most likely the page hasn't fully rendered and you are referencing an object that doesn't exist on the page.

September 9, 2015 at 7:31 am

Tried it. It doesn't work. I tried it with while($ie.busy) still nothing. The moment I run it in the command line it clicks the button in the modal box.
Any other suggestions?

September 9, 2015 at 7:49 am

If everything works step-by-step, then most likely you are just trying to call a method on something that doesn't exist. So, rather than doing your click like this:


you might want do something like:

while ($fileButton) {
    $fileButton = $ie.Document.getElementById("file")
    Sleep -Seconds 2

Then you are looping until the button is found (not null) versus using the Busy or ReadyState.

September 9, 2015 at 8:36 am

It is interesting when I try this loop
While ($ie.Document.getElementById("file")) {Sleep -Seconds 2}
it never finds that button.

September 9, 2015 at 10:10 am

Korhan, as Rob initially suggested, you might try using ReadyState rather than busy. According to MSDN, but just

Gets a value that indicates whether the object is engaged in a navigation or downloading operation.

Is it possible that the site content has finished downloading, but the browser has not yet finished rendering the page, and that is why you are getting the error.


Contains values that indicate what state an object is in.

End Enum

You could try:
while($ie.ReadyState -ne 4){Start-Sleep 1}

September 9, 2015 at 11:08 am

I have tried this too.
You cannot call a method on a null-valued expression.
At line:23 char:42
+ $ie.Document.getElementById("file").Click <<<< () + CategoryInfo : InvalidOperation: (Click:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull This is the error I am getting. Running the code in the command window and it opens that "Browse" window.

September 9, 2015 at 11:31 am

Interesting. Could try to rule out a timing issue by just putting a Start-Sleep -s 60 just before the command. That should give it plenty of time to load and settle then wait to see if it errors again. If it does, then it's very unlikely a timing issue.

September 9, 2015 at 11:42 am

I am wondering if this has anything to do with the "Browse" button being located on a modal box.
Does that make any difference?

September 9, 2015 at 11:44 am

It worked. I just forced it to sleep -s 100 and worked. Is there a way to check if the modal box is loaded? Just curiosity.

September 9, 2015 at 11:46 am

My bad, it is still not working. I though it worked but apparently it is not.

September 9, 2015 at 12:29 pm

I was using brackets {Start-Sleep -s 10}. When I got rid of the brackets, it worked.

September 9, 2015 at 1:22 pm

So it sounds like it is a timing thing. Also I just took a look at one of your previous posts.

It is interesting when I try this loop
While ($ie.Document.getElementById("file")) {Sleep -Seconds 2}
it never finds that button.

That should actually be a Not

While (-NOT ($ie.Document.getElementById("file"))) {Sleep -Seconds 2}

September 9, 2015 at 2:16 pm

OK. Well, I am new to PS and don't know the exact structure of the loops.
Thanks for letting me know.

September 9, 2015 at 2:30 pm

Does changing it to a NOT resolve the issue? As it was the process was looking and if it found the button, it would just sleep. That's the exact opposite if what you want. You want it to sleep if the button is not found and the look again. If you change that, you can probably get rid of the 10 second wait.

September 10, 2015 at 11:21 am

Thanks for the suggestion and correction.
I have not tried that version yet since it was working with forced Sleep command.