如何在 Selenium 中处理基本身份验证而不在 URL 中嵌入凭证?
在 Selenium 中处理基本身份验证(Basic Authentication),而不直接在 URL 中嵌入凭证(如 http://username:password@website.com),可以通过以下方法实现:
Alert 接口基本身份验证通常会触发浏览器的身份验证弹窗,可以使用 Selenium 的 Alert 接口来输入用户名和密码。
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BasicAuthExample {
public static void main(String[] args) {
// 设置 ChromeDriver 路径
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// 打开需要基本身份验证的 URL
driver.get("http://example.com/protected");
// 切换到弹窗并输入凭证
Alert alert = driver.switchTo().alert();
alert.sendKeys("username" + "\t" + "password"); // 使用 Tab 分隔用户名和密码
alert.accept(); // 确认登录
// 后续操作
System.out.println("Page Title: " + driver.getTitle());
driver.quit();
}
}
Alert 输入基本身份验证的凭证。为 Chrome 或 Firefox 创建一个扩展程序,自动为请求添加 Authorization 头。
创建 Chrome 扩展程序:
manifest.json 和 background.js 的文件夹。manifest.json 示例:
{
"manifest_version": 3,
"name": "Basic Auth Extension",
"version": "1.0",
"permissions": ["webRequest", "webRequestBlocking", "http://example.com/*"],
"background": {
"service_worker": "background.js"
}
}
background.js 示例:
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
details.requestHeaders.push({
name: "Authorization",
value: "Basic " + btoa("username:password")
});
return { requestHeaders: details.requestHeaders };
},
{ urls: ["http://example.com/*"] },
["blocking", "requestHeaders"]
);
加载扩展到 Selenium:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class BasicAuthWithExtension {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("path/to/extension.crx"));
WebDriver driver = new ChromeDriver(options);
driver.get("http://example.com/protected");
System.out.println("Page Title: " + driver.getTitle());
driver.quit();
}
}
通过代理工具拦截 HTTP 请求并注入 Authorization 头。
import com.github.tomakehurst.wiremock.client.BasicCredentials;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
import com.github.tomakehurst.wiremock.client.WireMock;
public class BasicAuthWithProxy {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--proxy-server=http://localhost:8080");
WebDriver driver = new ChromeDriver(options);
// 配置代理,注入 Authorization 头
WireMock.configureFor("localhost", 8080);
WireMock.stubFor(
WireMock.get(WireMock.urlPathMatching("http://example.com/protected"))
.withBasicAuth("username", "password")
);
driver.get("http://example.com/protected");
System.out.println("Page Title: " + driver.getTitle());
driver.quit();
}
}
Selenium Wire 或其他代理库。对于支持命令行参数的浏览器,可以在启动时传递身份验证信息。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class BasicAuthWithArgs {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/user/profile"); // 使用带有已登录状态的浏览器配置
options.addArguments("auth-server-whitelist=*example.com");
WebDriver driver = new ChromeDriver(options);
driver.get("http://example.com/protected");
System.out.println("Page Title: " + driver.getTitle());
driver.quit();
}
}
Alert 接口或 URL 嵌入凭证。
2025 年陆剧市场依旧热度爆棚
时间:2025-09-19
阵地央一首播:年度高品质大剧,深度解锁文化抗战三重非凡意义
时间:2025-09-18
陈展鹏刘佩玥《巨塔之后》今首播
时间:2025-08-28
生万物大结局惊现最招恨角色,原来真正的坏都披着善的“羊皮”!
时间:2025-08-28
归队6集燃爆:袁姗姗化身“战地玫瑰”,实战军医双在线超吸睛!
时间:2025-08-28
许凯田曦薇新剧《子夜归》首播,点击率位列第七
时间:2025-08-21