【问题标题】:PowerShell SendKeys to InternetExplorer ComObjectPowerShell SendKeys 到 InternetExplorer ComObject
【发布时间】:2016-03-25 03:58:00
【问题描述】:

这是我发现的一种方法,它将 IE ComObject 窗口置于前台并使用 SendKeys。

如何使用此方法发送一系列按键?

$ie = New-Object -ComObject InternetExplorer.Application
$ie.navigate("www.google.com")
do {sleep 1} until (-not ($ie.Busy))
Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
 using System;
 using System.Runtime.InteropServices;
 public class StartActivateProgramClass {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
 public static extern bool SetForegroundWindow(IntPtr hWnd);
 }
"@
$hWnd = $ie.HWND
$ie.Visible = $true
[void] [StartActivateProgramClass]::SetForegroundWindow($hWnd)
[System.Windows.Forms.SendKeys]::SendWait("a 2")

【问题讨论】:

    标签: shell powershell sendkeys


    【解决方案1】:

    您必须加载 Windows 窗体程序集才能使用 SendKeys。 这是一个例子

    [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
    
    $ie=new-object -com "internetexplorer.application"
    $ie.visible=$true
    $ie.navigate("http://someURI")
    
    #waiting for my form 
    while( ($ie.document.body.getElementsbytagname("input") |?{$_.type -eq 'file'}) -eq $null){
            start-sleep -milliseconds 100
        }
    # give the focus to ie
    [Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
    
    #send keys 
    [System.Windows.Forms.SendKeys]::Sendwait("{TAB}");   
    [System.Windows.Forms.SendKeys]::Sendwait(" ");   
    start-sleep 1
    [System.Windows.Forms.SendKeys]::Sendwait($file);         
    [System.Windows.Forms.SendKeys]::Sendwait("{TAB}");
    [System.Windows.Forms.SendKeys]::Sendwait("{ENTER}");
    

    我自己尝试这样做来自动化 IE 以进行一些性能测试,但 SendKeys 不是一种安全的方法,想象一个应用程序窃取了焦点,然后所有的密钥都发送到这个应用程序而不是 IE。我认为使用 Selenium 项目是可行的方法

    【讨论】:

    • 我希望事情就这么简单,IE 总是打开的,所以AppActivate 可能会关注错误的实例。这就是为什么在我的示例中MakeTopMost 作用于新创建的 IE ComObject 的 HWND。这对我还是一个新手脚本编写者没有帮助。另外,this article 提到 LoadWithPartialName 已过时,请改用 Add-Type。叫我困惑!
    • 你说你已经有一个工作方法可以将焦点放在你的 IE 实例上,所以应该能够通过 sendkeys 与之交互。
    • 是的,$app = Add-Type -MemberDefinition $signature -Name Win32Window -Namespace ScriptFanatic.WinAPI -ReferencedAssemblies System.Windows.Forms -Using System.Windows.Forms -PassThru 但是这是poshcode.org/1837 的结果,我在尝试使用SendKeys 方法时迷失了方向。
    • 另一种选择可能是使用来自 codeplex 的 wasp 模块。
    • 我见过 WASP 但想避免使用外部依赖项。我正在用更多借用的代码更新问题。
    【解决方案2】:

    我想看看 .NET sendkey 方法是否会单击带有我编写的脚本的按钮以自动将我登录到 https:网站。(最终我将使用任务计划程序进行计划,并让它在每天的某个时间自动执行)。我使用了 .NET 程序集中的 {Enter} 发送键。它对我有用,所以我决定分享,希望它能帮助别人。

    您可能需要检查您的 html 文档以查看 TagName 的具体类型并进行相应的编辑。

    $IE=new-object -com Internetexplorer.application

    $IE.navigate2("https://Yourwebsite.com")
    $IE.visible=$true
    do { sleep 15 }
    while ( $IE.busy )
    
    $Link=@($IE.Document.getElementsByTagName("input.")) | where-object {$_.type     -eq "submit"}
    $click=$Link.[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}");
    $click
    

    **** 您必须在 Powershell 脚本中添加 .NET 命名空间才能使用命名空间,否则它会给您报错 ****

    例如。 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 2022-06-18
      • 1970-01-01
      • 2012-12-01
      相关资源
      最近更新 更多