PowerShell SendKeys to InternetExplorer ComObject

纵然是瞬间 提交于 2021-02-10 15:56:47

问题


Here's one method I found which brings the IE ComObject window to the foreground and uses SendKeys.

How would I send a sequence of key presses using this method?

$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")

回答1:


You will have to load the windows form assembly to use SendKeys. here is an example

[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}");

I tried this myself to automate IE in order to make some performance tests, but SendKeys is not a safe way to do this, imagine an application steals the focus and then all the keys are send to this app instead of IE. Using Selenium project would be the way to go I think




回答2:


I wanted to see if .NET sendkey methods would click a button with a script I wrote to automatically log me into a https: website.(Eventually I will schedule with task scheduler and have it automate at a certain time everyday). I used the {Enter} send key from the .NET Assembly. It worked for me so I decided to share in hopes it would help someone.

You may have to inspect your html document to see what specific type the TagName is and edit accordingly.

$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

**** You have to add the .NET namespace in your Powershell script to use the namespace or it will give you and error ****

Ex. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")



来源:https://stackoverflow.com/questions/29531022/powershell-sendkeys-to-internetexplorer-comobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!