【问题标题】:IE Automation with Powershell send ENTER Key带有 Powershell 的 IE 自动化发送 ENTER 键
【发布时间】:2021-10-19 22:59:48
【问题描述】:

我正在使用 ServiceNow 进行 IE 自动化,其中有一个填充搜索数据的选项,但没有可用于使用 CLICK 方法的搜索按钮。因此,一旦我填写了搜索数据,我正在寻找输入 {ENTER} 或 {~} 之类的键的方法。但我正处于 PowerShell 脚本的中间阶段,不知道如何使用它。

如果有人可以帮助我的方法,将不胜感激。

$IE = New-Object -ComObject InternetExplorer.application
$IE.FullScreen = $false
$IE.Visible = $true
$IE.Navigate($ServiceNowURL)

While ($IE.Busy -eq $true)
{
    Start-Sleep -Milliseconds 50
}

$Enter = Read-Host 'To continue press ENTER'

#Enter
$Search = $IE.Document.IHTMLDocument3_getElementsByTagName('input') | ? {$_.id -eq 'sysparm_search'}
$EnterValue = $Search.value() = $TicketNumber

【问题讨论】:

  • 有没有办法自己测试?我需要 URL 和您尝试完成的步骤。
  • stackoverflow.com/questions/29531022/… - 也许这会有所帮助。
  • 您好,请问您的问题是否已解决?下面的my answer 对处理这个问题有帮助吗?如果有什么我可以在这里提供帮助的,请告诉我。
  • 今天需要测试一下,分享反馈

标签: powershell internet-explorer automation servicenow


【解决方案1】:

首先,您需要使用AppActivate 激活IE 窗口并将其置于最前面,然后使用focus() 将焦点设置到搜索区域。之后,您可以使用 SendKeys 发送 Enter 键。

我以https://www.google.com 为例进行搜索,您可以参考下面我的代码示例。我测试了它,它运行良好:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

$ie = New-Object -ComObject 'InternetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("https://www.google.com") #change it to your own url
while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
$search=$ie.Document.getElementsByName("q")[0] #change it to your own selector
$search.value="PowerShell" #change it to your own search value

Sleep 5
$ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id) 
$search.focus() 
[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}"); 

【讨论】:

    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 2014-02-09
    • 2017-10-02
    • 1970-01-01
    • 2012-03-15
    • 2016-09-30
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多