如何使用 Selenium 测试需要身份验证的页面?
我们可以使用 Selenium 测试需要身份验证的页面。为此,我们必须将用户的凭据发送到 URL。实际上,我们同时传递附加到 URL 的用户名和密码。
语法
https://username:password@URL https://admin:admin@the-internet.herokuapp.com/basic_auth
在此处,“admin”是用户名和密码。
URL – www.the-internet.herokuapp.com/basic_auth
让我们看看如何接受验证弹窗。

示例
代码实现。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestAuthnPopup{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String t = "admin";
// adding username, password with URL
String s = "https://" + t + ":" + t + "@" + "the-internet.herokuapp.com/basic_auth";
driver.get(s);
// identify and get text after authentication of popup
String m = driver.findElement(By.cssSelector("p")).getText();
System.out.println("Text after pop-up authentication: " + m);
driver.quit();
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP