使用 powershell 处理弹出框 [英] Handling a popup box using powershell

查看:337
本文介绍了使用 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,从而使您可以访问 FindWindowSetForegroundWindow.然后使用 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.

像这样注册方法:

$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:

这篇关于使用 powershell 处理弹出框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆