Search code examples
windowspowershellpowershell-5.0

"Unspecified Error" when calling getElementById()


I am getting this error when executing the below script. Output for the same is also mentioned. I did check for error but dint find any fix. I am using Windows 10 with PowerShell version: 5.0.10586.0. Request anyone's assistance in resolving through this error. I can only see the IE opening with the mentioned URL and the excutable file Notepad. Script unable to perform auto login.

Output:

PS C:\WINDOWS\system32> C:\Users\admin\Desktop\Test.ps1
Unspecified error.
At C:\Users\admin\Desktop\Test.ps1:35 char:1
+ $IE.Document.getElementById(“Email”).value = $Username
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Unspecified error.
At C:\Users\admin\Desktop\Test.ps1:36 char:1
+ $IE.Document.getElementById(“signIn”).Click()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Unspecified error.
At C:\Users\admin\Desktop\Test.ps1:37 char:1
+ $IE.Document.getElementByID(“Passwd”).value=$Password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Unspecified error.
At C:\Users\admin\Desktop\Test.ps1:38 char:1
+ $IE.Document.getElementById(“signIn”).Click()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Script:

# Edit this to be the URL or IP address of the site to launch on login
$Url = "www.gmail.com"

# Edit this to be the username
$Username= "[email protected]"

# Edit this to the corresponding password
$Password= "xxxxx"

# Edit this to be the path to the executable.  Include the executable 
# file name as well.
$Executable = "c:\windows\system32\notepad.exe"

# Invoke Internet Explorer
$IE = New-Object -com internetexplorer.application;
$IE.Visible = $true;
$IE.Navigate($url);

# Wait a few seconds and then launch the executable.
while ($IE.Busy -eq $true) {
  Start-Sleep -Milliseconds 5000;
}

# The following UsernameElement, PasswordElement, and LoginElement need
# to be modified first.  See the notes at the top of the script for more
# details.
$IE.Document.getElementById("Email").value = $Username
$IE.Document.getElementById("signIn").Click()
$IE.Document.getElementByID("Passwd").value=$Password
$IE.Document.getElementById("signIn").Click()

while ($IE.Busy -eq $true) {
  Start-Sleep -Milliseconds 5000;
}

Invoke-Item $Executable

Solution

  • If your mail address is not valid, you will never get the Password field, but an error message. For me it is working.