如何使用 Java 處理 Selenium WebDriver 中的驗證彈出視窗?
Dec 10, 2024 am 02:16 AM使用Java 使用Selenium WebDriver 處理身份驗證彈出視窗
在Selenium WebDriver 中,導航到需要身份驗證的頁面通常會觸發彈出視窗。但是,即使使用者輸入正確的憑證,問題中提供的程式碼仍會顯示身份驗證彈出視窗。這可能會令人沮喪並阻礙自動化工作。
解決方案在於利用 Alert 類別的authenticateUsing() 方法。此方法可讓您繞過基本的 HTTP 驗證彈出視窗並自動對使用者進行身份驗證。這是修改後的程式碼:
import org.openqa.selenium.Alert; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class EnhancedAuthenticationHandling { public static void main(String[] args) { // Set up Firefox profile to accept long username and password FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("network.http.phishy-userpass-length", 255); profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "x.x.x.x"); // Create FirefoxDriver with the modified profile WebDriver driver = new FirefoxDriver(profile); // Navigate to the protected page String login = "username"; String password = "password"; String url = "http://protectedpage.com"; String baseUrl = "http://" + login + ":" + password + "@" + url; driver.get(baseUrl + "/"); // Wait for authentication alert to appear WebDriverWait wait = new WebDriverWait(driver, 10); Alert alert = wait.until(ExpectedConditions.alertIsPresent()); // Authenticate using the provided credentials alert.authenticateUsing(new UserAndPassword(login, password)); } }
登入後複製
在此更新的程式碼中,使用正確的使用者名稱和密碼呼叫authenticateUsing()方法。這應該會自動驗證用戶身份並關閉彈出窗口,從而允許頁面成功加載。
注意:
- authenticateUsing() 方法僅支援Selenium WebDriver 版本 3.4 及更高版本,它仍然被認為是 beta 功能。
- 目前,它僅針對 InternetExplorerDriver 實作。
以上是如何使用 Java 處理 Selenium WebDriver 中的驗證彈出視窗?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林
兩個點博物館:所有展覽以及在哪裡可以找到它們
3 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林
兩個點博物館:所有展覽以及在哪裡可以找到它們
3 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

2025年的前4個JavaScript框架:React,Angular,Vue,Svelte

如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?

如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?

如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?
