首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell SendKeys to InternetExplorer ComObject

PowerShell SendKeys to InternetExplorer ComObject
EN

Stack Overflow用户
提问于 2015-04-09 06:10:30
回答 2查看 13.1K关注 0票数 1

下面是我找到的一种方法,它将IE ComObject窗口带到前台,并使用SendKeys。

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

代码语言:javascript
复制
$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")
EN

回答 2

Stack Overflow用户

发布于 2015-04-09 06:43:56

要使用SendKeys,必须加载windows窗体程序集。下面是一个例子

代码语言:javascript
复制
[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项目是可行的。

票数 1
EN

Stack Overflow用户

发布于 2016-03-21 11:22:06

我想看看.NET sendkey方法是否会用我编写的脚本单击按钮,自动登录到https:网站(最终,我将使用任务调度程序进行调度,并让它在每天某一时间自动完成)。我使用了来自.NET程序集的{Enter} send键。它为我工作,所以我决定分享,希望它能帮助某人。

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

$IE=新对象-com Internetexplorer.application

代码语言:javascript
复制
$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::LoadWithPartialName("System.Windows.Forms")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29531022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档