This is the script which I created:
add-type -AssemblyName System.Windows.Forms
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate("http://www.microsoft.com")
$ie.Navigate2("http://www.google.com" , 2048)
$ie.Navigate2("http://www.outlook.com" , 2048)
$ie.Navigate2("https://studer-ksg.intranet.koerber.de/de/nc/fritz-studer-ag.html" , 2048)
$ie.Visible = $true
start-sleep -Milliseconds 4000
[System.Windows.Forms.SendKeys]::SendWait('{F11}')
do
{
start-sleep -Milliseconds 4000
[System.Windows.Forms.SendKeys]::SendWait('^{TAB}')
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{F5}")
}
While ($i -ne 0)
It opens an IE window with 4 tabs in fullscreen and switches between them. When I start the script, the window opens but nothing happens until I click in it. How can I make this automatically so you only have to run the script and everything goes by itself?
You need to bring the IE Window to the front so it has focus before the SendKeys will work:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -Assembly "Microsoft.VisualBasic"
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate("http://www.microsoft.com")
$ie.Navigate2("http://www.google.com" , 2048)
$ie.Navigate2("http://www.outlook.com" , 2048)
$ie.Visible = $true
$IEProcess = Get-Process | Where { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($IEProcess.Id)
Start-Sleep -Milliseconds 4000
[System.Windows.Forms.SendKeys]::SendWait('{F11}')
do
{
Start-Sleep -Milliseconds 4000
[System.Windows.Forms.SendKeys]::SendWait('^{TAB}')
Start-Sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{F5}")
}
While ($i -ne 0)
Related
I wrote a script that works fine:
# Use Internet Explorer
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
# Set Credentials
$username="name.surname#mail.com"
$password="password"
#Navigate to URL
$ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
# Login
$usernamefield = $ie.document.getElementByID('isiwebuserid')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('isiwebpasswd')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('actionLogin')
$Link.click()
Start-Sleep -seconds 5
# Find file to download
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'}
$link.click()
Start-Sleep -seconds 3
# Press "Alt + s" on the download dialog
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s")
Start-Sleep -seconds 3
# Quit Internet Explorer
$ie.Quit()
But if I change $ie.Visible= $true to $ie.Visible= $false the script doesn't work.
Why?
Because of these two lines:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s")
In these two lines I'm working on the download dialog box of Internet Explorer and if the browser works in background the the script cannot click on it.
How can I send the input in background or in alternative how to keep Internet Explorer always on top?
As your own answer implies, in order to be able to send keystrokes to an application with [System.Windows.Forms.SendKeys]::SendWait(), it must have a window that is (a) visible and (b) has the (input) focus.
A simpler and faster alternative to the technique shown in your answer - where you use ad-hoc compilation of C# code that wraps WinAPI functions via P/Invoke declarations, via Add-Type - is the following:
# Create an Internet Explorer instance and make it visible.
$ie = New-Object -ComObject 'internetExplorer.Application'; $ie.Visible= $true
# Activate it (give it the focus), via its PID (process ID).
(New-Object -ComObject WScript.Shell).AppActivate(
(Get-Process iexplore | Where-Object MainWindowHandle -eq $ie.hWnd).Id
)
Taking a step back:
GUI scripting (automating a task by simulating user input to a GUI) is inherently unreliable; for instance, the user may click away from the window that is expected to have the focus.
While there is no built in solution, it sounds like Selenium offers robust programmatic browser control.
The third-party Selenium PowerShell module is a PowerShell-friendly wrapper for it (available via the PowerShell Gallery and therefore with Install-Module Selenium), but I don't know if it still works (the project is looking for maintainers as of this writing).
The closest thing I have found is this and it's ugly as hell:
# Start Internet Explorer on top
Add-Type -TypeDefinition #"
using System;
using System.Runtime.InteropServices;
public class Win32SetWindow {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"#
$ie = new-object -comobject InternetExplorer.Application;
$ie.visible = $true;
[Win32SetWindow]::SetForegroundWindow($ie.HWND) # <-- Internet Explorer window on top
# Set Credentials
$username="name.surname#mail.com"
$password="password"
#Navigate to URL
$ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
# Login
$usernamefield = $ie.document.getElementByID('isiwebuserid')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('isiwebpasswd')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('actionLogin')
$Link.click()
Start-Sleep -seconds 5
# Find file to download
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'}
$link.click()
Start-Sleep -seconds 3
# Press "Alt + s" on the download dialog
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%n{TAB}{ENTER}") # or use SendWait("%s")
Start-Sleep -seconds 3
# Quit Internet Explorer
$ie.Quit()
Start-Sleep -Seconds 5
$edge = (New-Object -COM "Shell.Application").Windows()| ? { $_.Name -like "*Microsoft Edge*" }
while(1) {
Start-Sleep -Seconds 10
try{
$edge.Refresh()
} catch { exit }
}
The code below should work. Consider using the Microsoft Edge Driver if you need to do more advanced automation.
$proc = [System.Diagnostics.Process]::Start("msedge.exe", "https://www.stackoverflow.com") | Get-Process
$wshell = New-Object -ComObject WScript.Shell
while ($true) {
Start-Sleep -Seconds 10
$wshell.AppActivate($proc.Id)
$wshell.SendKeys("{F5}")
}
I am trying to use powershell to automate opening IE and placing a value inside a text box and pressing a button.
Add-Type -AssemblyName Microsoft.VisualBasic
cls
$orderID = "text box entry"
$smoURL = "URL"
$iterator = 1;
Write-Host ("ID = $orderID")
$orderIDTextBox = "txtOrderID"
$searchButton = "btnSearch"
$IE = New-Object -ComObject InternetExplorer.Application
$ie.visible = $true
$ie.navigate("$smoURL")
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 2000}
Start-Sleep -Seconds 3
$ie.Document.getElementById("$orderIDTextBox").value = "$orderID"
$Submit = $ie.Document.getElementsByTagName("$btnSearch")
$Submit.click()
I've got the site to open but when I try populate the text field I get this error message:
This is the inspect element on the webpage.
I want to make a Script which opens IE with 4 tabs, goes to fullscreen and then switches between the tabs while constantly reloading the pages as soon as they get opened.
add-type -AssemblyName System.Windows.Forms
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate("http://www.microsoft.com")
$ie.Navigate2("http://www.google.com" , 2048)
$ie.Navigate2("http://www.outlook.com" , 2048)
$ie.Navigate2("http://www.vice.com" , 2048)
$ie.Visible = $true
[System.Windows.Forms.SendKeys]::SendWait("{F11}{ENTER}")
do
{
start-sleep -Milliseconds 4000
[System.Windows.Forms.SendKeys]::SendWait("{F5}{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait('^{TAB}')
[System.Windows.Forms.SendKeys]::SendWait("{F5}{ENTER}")
}
While ($i -ne 0)
With this current script, the browser and the tabs open and it also switches between tabs. The Problem is, for example, if it switches to google, microsoft gets refreshed instead of google.
The other problem is, that it doesn't go in to fullscreen. Is the command in the wrong place?
PS: This is my first PS Script
I have not tried the following, but from looking at your code, there are a few things strange:
There's no pause between showing the IE window and trying to make it fullscreen. I'd add that.
You're sending {ENTER} for no reason.
In your loop you first refresh the current tab, then switch tabs, then refresh the new tab. Why?
I'd change the code as follows:
add-type -AssemblyName System.Windows.Forms
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate("http://www.microsoft.com")
$ie.Navigate2("http://www.google.com" , 2048)
$ie.Navigate2("http://www.outlook.com" , 2048)
$ie.Navigate2("http://www.vice.com" , 2048)
$ie.Visible = $true
start-sleep -Milliseconds 1000
[System.Windows.Forms.SendKeys]::SendWait("{F11}")
do
{
start-sleep -Milliseconds 4000
[System.Windows.Forms.SendKeys]::SendWait('^{TAB}')
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{F5}")
}
While ($i -ne 0)
Pardon me if this is too simple a question, but I'm not finding anything in the help files or online so far regarding doing this. I'm opening up a new browser window to test the login/logout feature of a web based application, but I want to open the IE window in maximized mode. I could set the size as:
$ie.height = 1024
$ie.width - 768
But is there a keyword or anything that I can use to just open it up maximized automatically or would I need to query the screen size first and then fill in the values from that query?
/matt
(new-object -com wscript.shell).run("url",3)
Solved the Problem of starting IE maximized with following:
Function maxIE
{
param($ie)
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$ie.Width = $screen.width
$ie.Height =$screen.height
$ie.Top = 0
$ie.Left = 0
}
cls
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
maxIE $ie
while ($ie.busy) {sleep -milliseconds 50}
$ie.navigate("http://www.google.com")
I couldn't get any specific answer to work, but did get a combination to work. The order in which these are called is important else it doesn't work.
Full Example:
$ie = New-Object -Com "InternetExplorer.Application"
$urls = #("http://www.google.com","http://www.yahoo.com")
$ie.Visible = $true
CLS
write-output "Loading pages now..."
#Maximize IE window
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$ie.height = $screen.height
#Open webpages
foreach ($link in $urls) {
$ie.Navigate2($link, 0x1000)
}
#close first blank tab
$sa = New-Object -ComObject Shell.Application
$tab = $sa.windows() | Where {$_.Name -match 'Internet Explorer' -and $_.LocationName -eq ''}
$tab.quit()
If you have the PowerShell Community Extensions 1.2 (PSCX) installed on PowerShell 2.0, I have verified that this works:
Pscx\Start-Process IExplore.exe; Start-Sleep 3; $hwnd = Get-ForegroundWindow
$sig = #'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'#
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
[Win32.NativeMethods]::ShowWindowAsync($hwnd, 3)
It is a little dicey because it is using a wait (start-sleep) of 3 secs to wait for IE to open and then it uses a PSCX cmdlet to get the window handle of the foreground window. If you only have one instance of IExplore running then you can use this to get that handle:
#(Get-Process IExplore)[0].MainWindowHandle
PowerShell 2.0 is required for the Add-Type support that allows us to call down to the Win32 API.
BTW from a quick Bing search it seems that getting IE to start maximized is a pretty common problem. For instance, with Start-Process you can specify -WindowStyle Maximized but IE doesn't honor that.
Just incase anyone else needs help I ended up just calling up iexplore maximized and then connected to it. You need to sleep because it calls too fast to connect. I'm sure there is a better way but I couldn't figure it out.
start iexplore -WindowStyle maximized
Start-Sleep -seconds 1
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }
$ie.navigate("URL")
#We will use the Win32 API function ShowWindowAsync, and spawn an IE Window Maximized.
#Parameters can be used for ShowWindowAsync
$Hide = 0
$Normal = 1
$Minimized = 2
$Maximized = 3
$ShowNoActivateRecentPosition = 4
$Show = 5
$MinimizeActivateNext = 6
$MinimizeNoActivate = 7
$ShowNoActivate = 8
$Restore = 9
$ShowDefault = 10
$ForceMinimize = 11
#Specify an interwebs address :)
$URL="http://www.google.com/"
#Create internetexplorer.application object
$IE=new-object -com internetexplorer.application
#Set some parameters for the internetexplorer.application object
$IE.TheaterMode = $False
$IE.AddressBar = $True
$IE.StatusBar = $False
$IE.MenuBar = $True
$IE.FullScreen = $False
$IE.visible = $True
#Navigate to the URL
$IE.navigate2($URL)
#the C#-style signature of an API function
$code = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
#add signature as new type to PowerShell (for this session)
$type = Add-Type -MemberDefinition $code -Name myAPI -PassThru
#Magic:
$type::ShowWindowAsync($IE.HWND[0], $Maximized)