感謝這篇文章,以下代碼使用 MS IE 自動登錄到 URL 并且效果很好。前兩行在這里是最重要的。請參閱下文,了解當我更改它們時會發生什么。我想明確表示我不是專業的 Web 開發人員。我只是一個系統管理員,在編碼方面被迫破解我的方式。
PowerShell 呼叫 IE 時的作業代碼:
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$username="myname"
$RSAPIN="mypin"
$password="mypassword"
$ie.Navigate("https://www.tibia.com/mmorpg/free-multiplayer-online-role-playing-game.php")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('username')
$usernamefield.value = "$username"
$RSAPINfield = $ie.document.getElementByID('password_input')
$RSAPINfield.value = "$RSAPIN"
$passwordfield = $ie.document.getElementByID('secondary_password_input')
$passwordfield.value = "$password"
非作業代碼如下所示;如果我將代碼更改為呼叫 MS Edge,則會出現大量錯誤,我認為這是因為 PowerShell New-Object不支持 MS Edge?
$msedge = New-Object -ComObject 'internetExplorer.Application'
$msedge.Visible= $true # Make it visible
$username="myname"
$RSAPIN="mypin"
$password="mypassword"
$msedge.Navigate("https://www.tibia.com/mmorpg/free-multiplayer-online-role-playing-game.php")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $msedge.document.getElementByID('username')
$usernamefield.value = "$username"
$RSAPINfield = $msedge.document.getElementByID('password_input')
$RSAPINfield.value = "$RSAPIN"
$passwordfield = $msedge.document.getElementByID('secondary_password_input')
$passwordfield.value = "$password"
呼叫 MS Edge 而不是 IE 時出現的錯誤:
新物件:檢索具有 CLSID {00000000-0000-0000-0000-000000000000} 的組件的 COM 類工廠失敗,原因是以下錯誤:80040154 未注冊類(HRESULT 例外:0x80040154 (REGDB_E_CLASSNOTREG))。在 C:\Scripts\msedge.ps1:3 char:11
- $msedge = 新物件 -ComObject 'msedge.Application'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
- FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
在此物件上找不到屬性“可見”。驗證該屬性是否存在并且可以設定。在 C:\Scripts\msedge.ps1:4 char:1- $msedge.Visible= $true # 讓它可見
CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : PropertyNotFound You cannot call a method on a null-valued expression. AtC:\Scripts\msedge.ps1:8 字符:1
- $msedge.Navigate("myurl")
CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. AtC:\Scripts\msedge.ps1:12 字符:1
- $usernamefield = $msedge.document.getElementByID('username')
I am at a loss here. According to this post, PowerShell New-Object only supports IE's COM Automation.
uj5u.com熱心網友回復:
你不能像使用 PowerShell 腳本對 IE 所做的那樣自動化 Edge Chromium 瀏覽器。COM 自動化介面是針對 IE 的,所以它不適用于 Edge。此外,Edge 沒有這樣的介面。
推薦使用Selenium WebDriver來自動化 Edge。Selenium web driver 支持多種開發語言,您可以選擇所需的語言。
您也可以參考Selenium 的官方檔案了解如何開始使用 Selenium,并參考不同語言的代碼示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/419306.html
標籤:
