I want to use powershell to automate a web download request. But I got some errors that I can't fix.
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.visible = $true
$ie.Navigate("http://10.8.140.232/KPIReport/Admin/Login.aspx")
$usr_name = $ie.document.getElementById('TextBoxAccount')
$pwd = $ie.document.getElementById('TextBoxPassword')
$login_button = $ie.document.getElementById('ButtonLogin')
the first 3 lines can run successfully.
but from line 4, error occurs.
You cannot call a method on a null-valued expression.
At line:4 char:1
+ $usr_name = $ie.document.getElementById("TextBoxAccount")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
And $ie.document return null in command line.
and by the way. all the element id are existed in source html code.
I don't what happens here.
thanks for your help.
As the comment above, the error is caused by the DOM hasn't been fully loaded. I use While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;} after $ie.Navigate. You could try to add it in your code.
Related
Courtesy of this post, the following code auto-logs into a URL using MS IE and works great. The first two lines are the most important here. See below for what happens when I change them. I want to make it clear That I am not a professional web developer. I am only a system administrator forced to hack my way along when it comes to coding.
Working code when PowerShell calls IE:
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$username="myname"
$RSAPIN="mypin"
$password="mypassword"
$ie.Navigate("https://www.tibia.com/mmorpg/free-multiplayer-online-role-playing-game.php")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('username')
$usernamefield.value = "$username"
$RSAPINfield = $ie.document.getElementByID('password_input')
$RSAPINfield.value = "$RSAPIN"
$passwordfield = $ie.document.getElementByID('secondary_password_input')
$passwordfield.value = "$password"
Non-working code is shown below; If I change the code to call MS Edge instead, I get a raft of errors and I think it's because PowerShell New-Object doesn't support MS Edge?
$msedge = New-Object -ComObject 'internetExplorer.Application'
$msedge.Visible= $true # Make it visible
$username="myname"
$RSAPIN="mypin"
$password="mypassword"
$msedge.Navigate("https://www.tibia.com/mmorpg/free-multiplayer-online-role-playing-game.php")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $msedge.document.getElementByID('username')
$usernamefield.value = "$username"
$RSAPINfield = $msedge.document.getElementByID('password_input')
$RSAPINfield.value = "$RSAPIN"
$passwordfield = $msedge.document.getElementByID('secondary_password_input')
$passwordfield.value = "$password"
Errors seen when calling MS Edge instead of IE:
New-Object : Retrieving the COM class factory for component with CLSID
{00000000-0000-0000-0000-000000000000} failed due to the following
error: 80040154 Class not registered (Exception from HRESULT:
0x80040154 (REGDB_E_CLASSNOTREG)). At
C:\Scripts\msedge.ps1:3
char:11
$msedge = New-Object -ComObject 'msedge.Application'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
The property 'Visible' cannot be found on this object. Verify that the
property exists and can be set. At
C:\Scripts\msedge.ps1:4
char:1
$msedge.Visible= $true # Make it visible
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound You cannot call a method on a null-valued expression. At
C:\Scripts\msedge.ps1:8
char:1
$msedge.Navigate("myurl")
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
C:\Scripts\msedge.ps1:12
char:1
$usernamefield = $msedge.document.getElementByID('username')
I am at a loss here. According to this post, PowerShell New-Object only supports IE's COM Automation.
You can't automate the Edge Chromium browser like what you do with IE using PowerShell script. The COM automation interface are for IE, so it doesn't work with Edge. Besides, Edge doesn't have such interface.
It's recommended to use Selenium WebDriver to automate Edge. Selenium web driver supports many developing languages, you can choose the desired language.
You can also refer to the official doc of Selenium to learn how to get start with Selenium and refer to the code sample of different languages.
I'm practicing using PowerShell, and today I thought I'd write a script that logs me into a website. I have no use for this specific example, but I thought it would be good practice. I created it while referencing this. Here is what I have so far.
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Visible = $true
$username = "my#email"
$password = "password"
$ie.Navigate("https://squirrel.ws/login")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernameField = $ie.Document.getElementByID('UserEmail')
$usernameField.value = $username
$passwordField = $ie.Document.getElementByID('UserPassword')
$passwordField.value = $password
$link = $ie.Document.getElementByID('$0')
$link.click()
$ie.Quit()
When I run this, an IE window appears, and almost immediately closes with this error:
Method invocation failed because [System.DBNull] does not contain a method named 'click'.
At J:\removingForPrivacy\test.ps1 char:1
+ $link.click()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
So I'm pretty sure it throws an error either when trying to click, or because I labeled the button incorrectly, or both. When trying to create this, I couldn't find an ID for the button so I used what I could find. Otherwise I'm not sure why click() doesn't exist when it's in all the examples I'm seeing. Can anybody help me?
My issue came from the fact that when creating $Link, that I was using the wrong tags. I used all the same code, but swapped out the $Link creating line for:
$Link=$ie.Document.getElementsByTagName("button") | where-object {$_.type -eq "submit"}
I am trying to open a url in IE and then pass username and password to login to the url.But my code is not filling username and password
Following is code
$username = "userhere"
$password = "passhere"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$false
$ie.navigate("https://ameriprisestage.service-now.com/")
#while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("user_name").value= "$username"
$ie.document.getElementById("user_password").value = "$password"
Error: You cannot call a method on a null-valued expression. At line:7 char:1 + $ie.document.getElementById("user_name").value= "$username" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression. At line:8 char:1 + $ie.document.getElementById("user_password").value = "$password" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
There is a couple of things to note. First, your page is not ready yet. Yuo must wait for load. Your commented line was intended to do that. Second, both fields are in iframe which works as a separate sandbox - has his own document object. I also removed window hiding code to see results. Here is working code:
$username = "userhere"
$password = "passhere"
$ie = New-Object -com InternetExplorer.Application
$ie.navigate("https://ameriprisestage.service-now.com/")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById('gsft_main').contentWindow.document.getElementById('user_name').value = $username
$ie.document.getElementById('gsft_main').contentWindow.document.getElementById('user_password').value = $password
Make sure you close $ie object. You may have tons of them as background processes.
I have a web page with the following code relevant to the button:
<td width=300 align=left>Click here to run the batch tool:</td>
<td align='left' width=100><input id="Submit1" type="submit" value="Run" onClick='showSts();'/></td>
And my attempts to programmatically press it from Powershell using this code:
$username='username' $password='password'
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("name of Web site")
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
# $usernamefield = $ie.Document.getElementByID('memberemail')
# $usernamefield.value = $username
# $passwordfield = $ie.Document.getElementByID('memberpassword')
# $passwordfield.value = $password
$submitebutton=$ie.Documentelement.getElementByclassname('submit1') | Select-Object -First
$submitebutton.click()
Gives this error:
You cannot call a method on a null-valued expression. At
C:\Users\zkf4bi4\Documents\houdini_click.ps1:15 char:1
+ $submitebutton=$ie.Documentelement.getElementByclassname('submit1') | Select-Ob ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
C:\Users\zkf4bi4\Documents\houdini_click.ps1:16 char:1
+ $submitebutton.click()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any ideas on how to proceed? I'm an experienced developer but am new to Powershell
Well, I can't help but notice that you're calling GetElementsByClassName but 'Submit1' is the elements id. I suspect the error message is trying to tell you that $submitebutton is null. You might find this question an almost exact duplicate.
I am trying to write a basic powershell script to log into gmail but have some up against a problem. The code is below. I have commented out some bits to narrow things down.
$url = "http://gmail.com"
$username="myusername"
$password= "123456"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy)
{
Start-Sleep -m 10000;
}
$counter = 0
while (($counter -lt 10) -and ($ie.document -eq $null)) {Start-Sleep 1; $counter++}
$ie.document -eq $null
$ie.Document.getElementByID('email').value=$username
#$ie.Document.getElementByID("Password").value=$password
#$ie.Document.getElementById("signin").Click();
When I run the powershell script, I get the following error.
You cannot call a method on a null-valued expression.
At J:\scripts\gmail.ps1:21 char:1
+ $ie.Document.getElementByID('email').value=$username
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The strange thing is, this was working perfectly fine yesterday and today its not working at all. I am using IE9.
Any help much appreciated.
PowerShell IE9 ComObject has all null properties after navigating to webpage gave me the answer to this. Had to run my script as an administrator due to IE protected mode that was stopping the object from being created. Either turn off IE protected mode or run the script as an administrator to get it working.
I think you made a mistake here:
$ie.Document.getElementByID("Password").value=$password
The ID should be passwd, not password. So replace that line by:
$ie.Document.getElementByID("Passwd").value=$password
Works for me.