Can anyone please tell me how to click on \"ok\" or \"cancel\" on a popup window using powershell? I am trying to automate a website using powershell, but I am new to powershell
Try something like this:
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Vector RP1210 API Setup')
Sleep 1
$wshell.SendKeys('%C')
$wshell.AppActivate('Vector RP1210 API')
Sleep 1
$wshell.SendKeys('{ENTER}')
You might want to investige the WASP snapin from CodePlex:
http://wasp.codeplex.com/wikipage?title=Some%20Usage%20Examples&referringTitle=Home
With Powershell v2 you can use PInvoke to access the normal Win32 API, thus giving you access to FindWindow
and SetForegroundWindow
. Then use SendKeys
to send a Enter
key.
Something like this to register the methods:
$pinvokes = @'
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
'@
Add-Type -AssemblyName System.Windows.Forms
Add-Type -MemberDefinition $pinvokes -Name NativeMethods -Namespace MyUtils
Now you can find the window you need:
$hwnd = [MyUtils.NativeMethods]::FindWindow(null, "Popupboxname")
Give it focus:
[MyUtils.NativeMethods]::SetForegroundWindow($hwnd)
And send the Enter
key:
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Sources/inspiration: