问题描述
谁能告诉我如何使用 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.
像这样注册方法:
$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 处理弹出框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!