1

I'm trying to get my hands on filling in a form with PowerShell. So I am simply trying to do this:

$ieObject = New-Object -ComObject 'InternetExplorer.Application';
$ieObject.Visible = $true;;
$ieObject.Navigate('https://www.randomizer.org/');
$currentDocument = $ieObject.Document;
$inputbox = $currentDocument.getElementByID('randSets');
$inputbox.value = "My Value"; 

However this gives me the error

You cannot call a method on a null-valued expression.
At line:5 char:1
+ $inputbox = $currentDocument.getElementByID("randSets");

I don't exactly know why but my ieObject doesn't have all the properties (I only have 9 when I'm supposed to have around 50)and methods it should have. When I use Get-Member I can't see Document in there so is it normal or am I doing something really wrong?

PS C:\Users\n> $ieObject | Get-Member

   TypeName: System.__ComObject

Name                      MemberType Definition                                            
----                      ---------- ----------                                            
CreateObjRef              Method     System.Runtime.Remoting.ObjRef CreateObjRef(type re...
Equals                    Method     bool Equals(System.Object obj)                        
GetHashCode               Method     int GetHashCode()                                     
GetLifetimeService        Method     System.Object GetLifetimeService()                    
GetType                   Method     type GetType()                                        
InitializeLifetimeService Method     System.Object InitializeLifetimeService()             
ToString                  Method     string ToString()                                     

Thank you in advance!


I am using:

  • Windows 10 Enterprise (so I'm thinking maybe my company blocked some features, I will check with my IT department)
  • Powershell version is 5.1
  • Internet explorer version 11.885

EDIT: as suggested in the comment I tried to use VBA to do the same thing as my powershell script and I got the same error on the Document object Method 'Document' of object 'IWebBrowser2' failed'. This means I can't access properly my Internet Explorer application. In this case I think I should wait for the final answer of my IT department and then add more to this.

So I have access to the object and every property of it until I use Navigate. Then I get errors even for Get-Member like:

Get-Member : The following exception occurred while retrieving the string representation 
for property "Application" : "The RPC server is unavailable. (Exception from HRESULT: 
0x800706BA)"
17
  • I've seen this behavior with IE a number of times but haven't been able to replicate it across devices. If you need an automation engine, I would not recommend using the IE COM object at all but instead opt for something like selenium instead. Aug 12, 2019 at 13:05
  • @TheIncorrigible1 thanks, I will take a look at that
    – Jack
    Aug 12, 2019 at 14:04
  • @TheIncorrigible1 Selenium seems interesting however I would prefer something that I don't have to install so that's why I chose PowerShell in the first place. Thank you for the tip though!
    – Jack
    Aug 12, 2019 at 14:28
  • Using modules should not be frowned upon as it's a normal part of the software development process. You could always bundle the Selenium dll with your script for use. Aug 12, 2019 at 14:37
  • 1
    @jrider Please don't assume you know who downvoted. OP probably didn't. Aug 12, 2019 at 20:38

3 Answers 3

0

What looks like is happening is the page has not loaded by the time you are calling $currentDocument = $ieObject.Document; causing $currentDocument to be $null.

You should be able to fix it by using .Busy

$ieObject = New-Object -ComObject 'InternetExplorer.Application'
$ieObject.Visible = $true
$ieObject.Navigate('https://www.randomizer.org/');
do{
   Start-Sleep -Milliseconds 10
}while($ieObject.Busy)
$currentDocument = $ieObject.Document
$inputbox = $currentDocument.getElementByID('randSets')
2
  • @JackA when I ran this, I was able to access $inputbox variable.
    – jrider
    Aug 12, 2019 at 15:31
  • I will try to restart everything and see if the problem gets solved. And as mentioned in the edit of the original post I will ask my IT department.
    – Jack
    Aug 13, 2019 at 8:12
0

This is working here:

$ieObject = New-Object -Com InternetExplorer.Application

$ieObject.Visible = $true
[void]$ieObject.Navigate2('https://www.randomizer.org/')

while ($ieObject.busy) {
        sleep -seconds 1
}

$currentDocument = $ieObject.Document
$inputbox = $currentDocument.getElementByID('randSets')
$inputbox.value = "My Value"

You have to wait till the browser has loaded all objects.

0

So the issue was actually very simple, so simple I didn't think about it... I have to run the programs as administrator and then it works.

Thank you all for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.