opening webpage with powershell in IE hangs - powershell

I try to open a webpage with Powershell code in order to print the page with PDFCreator. It hangs when I try to run it; probably when opening internet explorer.
$ie = new-object -com internetexplorer.application
if ($showWindow) {
$ie.Visible = $true
} else {
$ie.Visible = $false
}
$ie.navigate($WebSiteURL)
Why does this hang and how can I fix this?

Related

Get URL of page in Internet Explorer using Powershell

I have a PowerShell script which will open an instance of internet explorer, go to a certain website and find a hidden link but when I go to that link it redirects me to another page.
I'd like to find the URL of the page that internet explorer is now in to be stored in a variable.
Thanks!
For those who only read code:
$IE = new-object -com internetexplorer.application
$IE.visible = $true
$Document = $IE.navigate2("example.com")
# Do stuff
$Document = $IE.navigate2($LinkIFound)
# It redirects me...
# TODO: Find URL of page I am now in.
So if you are trying to get the current location of a document then you can use : $IE.Document.url
$IE = new-object -com internetexplorer.application
$IE.visible = $true
$Document = $IE.navigate2("example.com")
# Do stuff
$OldUrl = $IE.Document.url
$Document = $IE.navigate2($LinkIFound)
sleep -seconds 3
$NewUrl = $IE.Document.url

Powershell startup script to login to website acting differently on restart

I have a powershell script I'm running on startup to open a website and log in. It works fine whenever I run the script manually, or whenever I sign out of my profile in Windows and sign back in. However, when I restart the computer, the script opens the website, but then fails to edit the DOM to change the username and password text fields and click the submit button. It seems to attempt to change the values because the cursor stops blinking, but nothing happens.
Here is the code.
$IEProcess = [System.Diagnostics.Process]::Start("iexplore", "-k https://www.website.com")
Sleep -Seconds 1
$IE = $(New-Object -ComObject "Shell.Application").Windows() | ? {$_.HWND -eq $IEProcess.MainWindowHandle}
while ($IE.ReadyState -ne 4)
{
Start-Sleep -Milliseconds 100
}
$IE.Document.getElementById(“userNameInput”).value = $Username
$IE.Document.getElementByID(“passwordInput”).value= $Password
$IE.Document.getElementById(“submitButton”).Click()
Using this code, I was able to get it to work. It may have to do with how you are configuring it to run on startup.
$Url = "https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%
2f%2fstackoverflow.com%2f"
$Username="name#email.com"
$Password="password"
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true;
$IE.navigate($url);
while ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 2000;
}
$IE.Document.getElementById("email").value = $Username
$IE.Document.getElementByID("password").value=$Password
$IE.Document.getElementById("submit-button").Click()
I did the following:
Open gpedit.msc
User config > Windows Settings > Scripts (Logon/Logoff)
"PowerShell Scripts" tab
Add script

Powershell IE login automation unable to find the element by ID

Can someone knowledgeable in powershell tell me why I can't find the username input field using its ID "user_name"? I have tried this on other websites and it has worked but for this one it just refuses to find it. I also tried the IHTMLDocument3_getElementsByName method and also IHTMLDocument3_getElementsByTagName looking for things like "input" or even "div". Some div classes pop up but so many are missing there and I have no idea why its missing. Please show me the light.
#ID names
$IDuserName = "user_name"
$IDuserPass = "user_password"
$url = "https://isssupport.service-now.com/"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$ie.navigate($URL)
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
$ie.Document.IHTMLDocument3_getElementByID($IDuserName).Value = $userName
Below is the HTML line for the Username input field
<input name="user_name" id="user_name" type="text" class="form-control" value="" autocomplete="off">
The page is built with loads of javascript. If you try opening the page without javascript enabled in your browser you won't even see the login form. this seems to be the problem for IE/PowerShell to find the form and its elements.
However you could bring the Internet Explorer window into foreground and interact with the page using Systems.Windows.Forms like this:
#ID names
$IDuserName = "user_name"
$IDuserPass = "user_password"
$url = "https://isssupport.service-now.com/"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($ie.Name)
Start-Sleep -Milliseconds 500
$ie.navigate($url)
While ($ie.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('username');
[System.Windows.Forms.SendKeys]::SendWait('{TAB}');
[System.Windows.Forms.SendKeys]::SendWait('password');
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}');
May I ask what the purpose of the script you are building is?
Best regards
Guenther

Click button on wordpress login page

I have a problem to click the login button on the following page:
https://community.theme.co/wp/wp-login.php
I have tried to click the button with code below:
$submitButton = $ie.document.getElementById("wp-submit").Click();
foreach ($element in $submitButton)
{
if ($element.Name -eq "wp-submit") {
$element.Click()
}
}
I think checking ie object's busy property might fix it.
$url= "https://community.theme.co/wp/wp-login.php"
$ie = new-object -ComObject "internetexplorer.application"
$ie.Navigate2($url)
$ie.Visible =$true
while($ie.Busy){
start-sleep 10
}
$submitButton = $ie.document.getElementById("wp-submit").click();
could not confirm as i don't have any valid credential but i think the click was happening

Power Shell updating property

I play around with Power Shell... And I have a newbie question to navigate with ie.
Have this Code:
# IE window
$ie = New-Object -com "InternetExplorer.Application"
$ie.visible = $true
function waitforpageload {
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 1000; }
}
# navigate to the first page
$ie.Navigate("http://ss64.com/bash/")
waitforpageload
$ie.Document.Url # return http://ss64.com/bash/
# navigate to the second page
$ie.Navigate("http://ss64.com/ps/")
waitforpageload
$ie.Document.Url # return also http://ss64.com/bash/
and I'm wondering why $ie.Document.Url in both times return http://ss64.com/bash/
should is it possible to get http://ss64.com/ps/ in the second call?
Thanks a lot
You'll get the current location using
$ie.LocationURL
To get a list of all methods and properties available, use $ie | gm