使用Powershell处理弹出框 [英] Handling a popup box using powershell
问题描述
有人可以告诉我如何使用Powershell在弹出窗口中单击确定"或取消"吗? 我正在尝试使用Powershell自动化网站,但是我是PowerShell的新手.我必须在弹出框中单击确定"按钮才能继续.我知道VBscript,因为我可以使用
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. I have to click on OK button in a popup box to proceed. I know VBscript, in that I can use
set obj0 = createobject("wscript.shell")
count = 0
do while count = 0
if obj0.appactivate "Popupboxname" then
----perform required action---
count = 1
else
wscript.sleep(2000)
end if
loop
有人可以告诉我如何在Powershell中执行相同的操作吗?如果我能以某种方式访问弹出窗口,则至少可以使用sendkeys命令发送回车键.请让我知道如何处理弹出窗口.预先感谢.
Can anyone tell me how to do the same in powershell? If i can somehow access the popup window, atleast i can use sendkeys command to send the enter key. Please let me know how to handle the popup window. Thanks in advance.
推荐答案
使用Powershell v2,您可以使用PInvoke访问常规的Win32 API,从而使您可以访问FindWindow
和SetForegroundWindow
.然后使用SendKeys
发送Enter
密钥.
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")
给予关注:
[MyUtils.NativeMethods]::SetForegroundWindow($hwnd)
并发送Enter
密钥:
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
来源/灵感:
Sources/inspiration:
- power shell:如何发送鼠标中键?
- http://poshcode.org/1837
- http://www.tek-tips.com/viewthread.cfm ?qid = 1563429
- power shell : how to send middle mouse click?
- http://poshcode.org/1837
- http://www.tek-tips.com/viewthread.cfm?qid=1563429
这篇关于使用Powershell处理弹出框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!