Chúng ta có thể xử lý popup xác thực (authentication popup) trong Selenium WebDriver bằng Java. Để làm điều này, ta phải truyền thông tin đăng nhập ngay trong URL. Ta thêm username và password vào URL.
Cú pháp:
https://username:password@URL https://admin:admin@the-internet.herokuapp.com/basic_auth Ở đây, admin là username và password. URL – www.the-internet.herokuapp.com/basic_auth Hãy làm việc và chấp nhận popup xác thực dưới đây.

Ví dụ
Triển khai code.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AuthnPopup {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String u = "admin";
// thêm username, password vào URL
String str = "https://" + u + ":" + u + "@" + "the-internet.herokuapp.com/basic_auth";
driver.get(str);
// xác định và lấy text sau khi xác thực popup
String t = driver.findElement(By.cssSelector("p")).getText();
System.out.println("Text is: " + t);
// đóng trình duyệt
driver.close();
}
}
Kết quả
