我寫了一個運行良好的腳本:
# Use Internet Explorer
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
# Set Credentials
$username="[email protected]"
$password="password"
#Navigate to URL
$ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
# Login
$usernamefield = $ie.document.getElementByID('isiwebuserid')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('isiwebpasswd')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('actionLogin')
$Link.click()
Start-Sleep -seconds 5
# Find file to download
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'}
$link.click()
Start-Sleep -seconds 3
# Press "Alt s" on the download dialog
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s")
Start-Sleep -seconds 3
# Quit Internet Explorer
$ie.Quit()
但是,如果我更改$ie.Visible= $true為$ie.Visible= $false腳本不起作用。
為什么?
因為這兩行:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s")
在這兩行中,我正在處理 Internet Explorer 的下載對話框,如果瀏覽器在后臺運行,則腳本無法單擊它。
如何在后臺發送輸入,或者如何讓 Internet Explorer 始終保持在最前面?
uj5u.com熱心網友回復:
正如您自己的答案所暗示的那樣,為了能夠將擊鍵發送到具有 的應用程式,[System.Windows.Forms.SendKeys]::SendWait()它必須具有一個 (a)可見且 (b) 具有 (輸入)焦點的視窗。
您的答案中顯示的技術的更簡單和更快的替代方法 - 您使用通過 P/Invoke 宣告包裝 WinAPI 函式的 C# 代碼的臨時編譯,通過Add-Type- 如下:
# Create an Internet Explorer instance and make it visible.
$ie = New-Object -ComObject 'internetExplorer.Application'; $ie.Visible= $true
# Activate it (give it the focus), via its PID (process ID).
(New-Object -ComObject WScript.Shell).AppActivate(
(Get-Process iexplore | Where-Object MainWindowHandle -eq $ie.hWnd).Id
)
退后一步:
GUI 腳本(通過模擬用戶對 GUI 的輸入來自動執行任務)本質上是不可靠的;例如,用戶可以點擊遠離預期具有焦點的視窗。
雖然沒有內置解決方案,但聽起來Selenium提供了強大的程式化瀏覽器控制。
- 第三方
SeleniumPowerShell 模塊是它的 PowerShell 友好型包裝器(可通過 PowerShell Gallery 獲得,因此與Install-Module Selenium),但我不知道它是否仍然有效(在撰寫本文時,該專案正在尋找維護者)。
- 第三方
uj5u.com熱心網友回復:
我發現的最接近的是這個,它非常丑陋:
# Start Internet Explorer on top
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Win32SetWindow {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
$ie = new-object -comobject InternetExplorer.Application;
$ie.visible = $true;
[Win32SetWindow]::SetForegroundWindow($ie.HWND) # <-- Internet Explorer window on top
# Set Credentials
$username="[email protected]"
$password="password"
#Navigate to URL
$ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
# Login
$usernamefield = $ie.document.getElementByID('isiwebuserid')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('isiwebpasswd')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('actionLogin')
$Link.click()
Start-Sleep -seconds 5
# Find file to download
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'}
$link.click()
Start-Sleep -seconds 3
# Press "Alt s" on the download dialog
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%n{TAB}{ENTER}") # or use SendWait("%s")
Start-Sleep -seconds 3
# Quit Internet Explorer
$ie.Quit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/418343.html
標籤:
下一篇:kafka學習筆記讀取佇列資料
