我寫了一個腳本,運行良好:
我寫了一個腳本,運行良好。
# Use Internet Explorer
$ie = New-Object -ComObject 'InternetExplorer.Application'
$ie.Visible= $true # Make it visible
# 設定證書 # 設定證書
$username="[email protected]"/span>
$password="password"/span>
#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()
開始-睡眠-秒5。
# 找到要下載的檔案。
$link = $ie.Document。 getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'} 。
$link.click()
Start-Sleep -seconds 3.
# 在下載對話框中按 "Alt s"。
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s"/span>)
Start-Sleep -seconds 3
# 退出Internet Explorer。
$ie.Quit()
但是如果我把$ie.Visible= $true改為$ie.Visible= $false,腳本就不作業了。
為什么?
因為這兩行:
Add-Type -AssemblyName System.Windows.Forms。
[System.Windows.Forms.SendKeys]::SendWait("%s"/span>)
在這兩行中,我在Internet Explorer的下載對話框中作業,如果瀏覽器在后臺作業,腳本就不能點擊它。
我怎樣才能在后臺發送輸入資訊,或者說怎樣才能使 Internet Explorer 始終處于頂部?
uj5u.com熱心網友回復:
正如你自己的答案所暗示的,為了能夠向一個應用程式發送按鍵用 與您答案中所示的技術相比,一個更簡單、更快速的替代方案--您使用臨時編譯的 C# 代碼,通過 P/Invoke 宣告包裝 WinAPI 函式,通過 退一步說: GUI腳本(通過模擬用戶對GUI的輸入來實作任務的自動化)本質上是不可靠的;例如,用戶可能會點擊離開預期擁有焦點的視窗。
uj5u.com熱心網友回復: 我找到的最接近的東西是這個,它丑得要命:
標籤: 上一篇:使用連接來獲取特定的資料集
[System.Windows.Forms.SendKeys]::SendWait(),它必須有一個(a)可見的和(b)具有(輸入)焦點的視窗。
Add-Type--如下: # 創建一個Internet Explorer實體并使其可見。
$ie = New-Object -ComObject 'internetExplorer'. Application'; $ie.Visible= $true.
# 激活它(給它焦點),通過它的PID(行程ID)。。
(New-Object -ComObject WScript.Shell).AppActivate(
(Get-Process iexplore | Wheree-Object MainWindowHandle -eq $ie.hWnd) .Id
)
Selenium PowerShell 模塊是它的 PowerShell 友好包裝(可通過 PowerShell Gallery獲得,因此可使用 Install-Module Selenium),但我不知道它是否仍然有效(截至本文撰寫時該專案正在尋找維護人員)。# Start Internet Explorer on top
Add-Type -TypeDefinition @"
using System.Runtime.Interface
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視窗在頂部。
# Set Credentials[/span]。
$username="[email protected]"/span>
$password="password"/span>
#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()
開始-睡眠-秒5。
# 找到要下載的檔案。
$link = $ie.Document。 getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'} 。
$link.click()
Start-Sleep -seconds 3.
# 在下載對話框中按 "Alt s"。
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%n{TAB}{ENTER}") # 或使用SendWait("%s")
Start-Sleep -seconds 3
# 退出Internet Explorer。
$ie.Quit()
