受這個帖子的啟發,下面的代碼可以使用MS IE自動登錄到一個URL,而且效果很好。 前兩行是這里最重要的。 請看下面,當我改變它們時會發生什么。 我想說明的是,我不是一個專業的網路開發人員。 我只是一個系統管理員,當涉及到編碼時,我不得不用黑客手段來解決。
當PowerShell呼叫IE時的作業代碼:
$ie = New-Object -ComObject 'InternetExplorer.Application'>
$ie.Visible= $true # Make it visible
$username="myname"
$RSAPIN="mypin"/span>
$password="mypassword"。
$ie.Navigate("https://www.tibia.com/mmorpg/free-multiplayer-online-role-playing-game.php"/span>)
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"/span>
$password="mypassword"。
$msedge.Navigate("https://www.tibia.com/mmorpg/free-multiplayer-online-role-playing-game.php"/span>)
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時看到的錯誤:
New-Object : 檢索組件的COM類工廠,CLSID為 {00000000-0000-0000-00000000}的組件的COM類工廠檢索失敗,原因如下 error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). 在 C:Scriptsmsedge.ps1:3 char:11
- $msedge = New-Object -ComObject 'msedge.Application'/li>
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException 。
- FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
。 在這個物件上找不到屬性'Visible'。核實該 屬性存在并且可以被設定。在 C:Scriptsmsedge.ps1:4 char:1
- $msedge.Visible= $true # 使其可見
-
CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : PropertyNotFound 你不能呼叫一個方法 關于一個空-值的表達。At
C:Scriptsmsedge.ps1:8 char:1
- $msedge.Navigate("myurl")
-
CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : InvokeMethodOnNull 你不能呼叫一個方法 關于一個null-值表達。At
C:Scriptsmsedge.ps1:12 char:1
- $usernamefield = $msedge.document.getElementByID('username')
-
我在這里感到很茫然。 根據這個帖子,PowerShell New-Object只支持IE的COM Automation。
uj5u.com熱心網友回復:
你不能像使用PowerShell腳本對IE那樣對Edge Chromium瀏覽器進行自動化。COM 自動化介面是針對 IE 的,所以它不能用于 Edge。此外,Edge 也沒有這樣的介面。
建議使用Selenium WebDriver來實作 Edge 自動化。Selenium Web 驅動程式支持許多開發語言,您可以選擇所需的語言。
您還可以參考Selenium 的官方檔案,了解如何開始使用 Selenium 并參考不同語言的代碼樣本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/320185.html
標籤:
