我正在使用 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
uj5u.com熱心網友回復:
首先,您需要使用 激活 IE 視窗并將其置于最前面AppActivate,然后使用 將焦點設定到搜索區域focus()。之后,您可以使用 發送Enter鍵SendKeys。
我以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}");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/419315.html
標籤:
