【问题标题】:Handling a popup box using powershell使用 powershell 处理弹出框
【发布时间】:2019-09-23 23:52:09
【问题描述】:

谁能告诉我如何使用 powershell 在弹出窗口上单击“确定”或“取消”? 我正在尝试使用 powershell 自动化网站,但我是 powershell 新手。我必须单击弹出框中的“确定”按钮才能继续。我知道VBscript,因为我可以使用

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 命令发送回车键。请让我知道如何处理弹出窗口。提前致谢。

【问题讨论】:

    标签: shell powershell popup automation


    【解决方案1】:

    使用 Powershell v2,您可以使用 PInvoke 访问普通的 Win32 API,从而使您可以访问 FindWindowSetForegroundWindow。然后使用SendKeys 发送Enter 密钥。

    像这样注册方法:

    $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
    

    现在你可以找到你需要的窗口了:

    $hwnd = [MyUtils.NativeMethods]::FindWindow(null, "Popupboxname")
    

    把重点放在:

    [MyUtils.NativeMethods]::SetForegroundWindow($hwnd)
    

    并发送Enter 密钥:

    [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
    

    来源/灵感:

    【讨论】:

      【解决方案2】:

      试试这样的:

      $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}')
      

      【讨论】:

        【解决方案3】:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-13
          • 1970-01-01
          • 2020-06-10
          • 1970-01-01
          • 1970-01-01
          • 2013-02-03
          • 2013-05-04
          • 2015-11-14
          相关资源
          最近更新 更多