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:
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)"
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')
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.
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!
User contributions licensed under CC BY-SA 3.0