我正在用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'/span>
#Enter #Enter
$Search = $IE.Document.IHTMLDocument3_getElementsByTagName('input') |? {$_.id -eq 'sysparm_search'}?
$EnterValue = $Search.value() = $TicketNumber[/span
uj5u.com熱心網友回復:
首先,你需要激活IE視窗并使用AppActivate將其帶到前面,然后使用focus()將焦點設定到搜索區域。之后,你可以使用SendKeys發送Enter鍵。
我使用https://www.google.com來搜索作為例子,你可以參考我下面的代碼示例。我測驗了一下,效果很好:
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms"/span>)
[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" #改變它為你自己的搜索值。
睡眠5
$ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id)
$search.focus()
[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}"/span>)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/320194.html
標籤:
