Running script separately does not give any error but whole script does not work

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 = “Sign in to Partner Home
$ie.navigate($url)
while($ie.busy){Start-Sleep 1}

Enter login information

$ie.Document.getElementById(“js-username”).value = “username”
$ie.Document.getElementById(“password_field”).value = “password”
$ie.Document.getElementById(“submit_button”).Click()
while($ie.busy){Start-Sleep 1}

Click Inventory Management under Inventory & EDI

$link = $ie.Document.getElementsByTagName(‘a’) | Where-Object {$_.innerText -eq ‘Inventory Management’}
$link.Click()
while($ie.busy){Start-Sleep 1}

Click on Import Inventory Data as button to upload excel file, then upload file

$ie.Document.getElementById(“uploadlink”).Click()
while($ie.busy){Start-Sleep -s 1}

Last line which gives the error

$ie.Document.getElementById(“file”).Click()

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.

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.

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?

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:

$ie.Document.getElementById("file").Click()

you might want do something like:

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

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

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

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.

ReadState

Contains values that indicate what state an object is in.

Syntax
Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum

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

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.

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.

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

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.

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

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

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}

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

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.

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