Basic HTTP Authentication
If you are testing a website that is protected with Basic HTTP Authentication, then your automated test might get stuck when opening the URL.
There are two approaches to handle this: the modern Selenium 4 BiDi approach (recommended) or the legacy URL credentials approach.
URL Credentials (Legacy Approach)
You can pass the username and password in the URL. Note that this approach exposes credentials in browser history and logs.
driver.get("https://<username>:<password>@www.example.com/password.html");
driver.get("https://<username>:<password>@www.example.com/password.html")
await driver.get("https://<username>:<password>@www.example.com/password.html");
driver.navigate.to "https://<username>:<password>@www.example.com/password.html"
$web_driver->get("https://<username>:<password>@www.example.com/password.html");
driver.Navigate().GoToUrl("https://<username>:<password>@www.example.com/password.html");
Selenium 4 with BiDi (Recommended)
Selenium 4 introduced the BiDi protocol which provides a cleaner way to handle Basic Authentication without exposing credentials in the URL.
This approach intercepts the authentication request and programmatically provides the credentials, keeping them out of browser history and logs.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.InterceptPhase;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.UsernameAndPassword;
// Enable BiDi with webSocketUrl capability
capabilities.setCapability("webSocketUrl", true);
WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
driver = new Augmenter().augment(driver);
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails -> network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("username", "password")));
driver.get("https://httpbin.org/basic-auth/user/passwd");
}
from selenium import webdriver
from selenium.webdriver.common.bidi.network import Network
options = webdriver.ChromeOptions()
options.enable_bidi = True
options.set_capability('tb:options', {
'selenium-version': '4.33.0'
})
driver = webdriver.Remote(
command_executor='https://key:secret@hub.testingbot.com/wd/hub',
options=options)
# Initialize BiDi network module and add authentication handler
network = Network(driver)
network.add_authentication_handler(
username='username',
password='password'
)
driver.get('https://httpbin.org/basic-auth/user/passwd')
driver.quit()
const { Builder } = require('selenium-webdriver');
const driver = await new Builder()
.withCapabilities({
browserName: 'chrome',
webSocketUrl: true,
'tb:options': {
'selenium-version': '4.33.0'
}
})
.usingServer('https://key:secret@hub.testingbot.com/wd/hub')
.build();
await driver.network().addAuthenticationHandler('username', 'password');
await driver.get('https://httpbin.org/basic-auth/user/passwd');
await driver.quit();
require 'selenium-webdriver'
options = Selenium::WebDriver::Chrome::Options.new
options.web_socket_url = true
options.add_option('tb:options', {
'selenium-version' => '4.33.0'
})
driver = Selenium::WebDriver.for(
:remote,
url: 'https://key:secret@hub.testingbot.com/wd/hub',
capabilities: options)
driver.register(username: 'username', password: 'password')
driver.navigate.to 'https://httpbin.org/basic-auth/user/passwd'
driver.quit
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
var options = new ChromeOptions();
options.AddAdditionalOption("webSocketUrl", true);
options.AddAdditionalOption("tb:options", new Dictionary<string, object>
{
["selenium-version"] = "4.33.0"
});
var driver = new RemoteWebDriver(
new Uri("https://key:secret@hub.testingbot.com/wd/hub"),
options.ToCapabilities());
INetwork network = driver.Manage().Network;
NetworkAuthenticationHandler handler = new NetworkAuthenticationHandler()
{
UriMatcher = (uri) => true,
Credentials = new PasswordCredentials("username", "password")
};
network.AddAuthenticationHandler(handler);
await network.StartMonitoring();
driver.Navigate().GoToUrl("https://httpbin.org/basic-auth/user/passwd");
await network.StopMonitoring();
driver.Quit();
For more BiDi examples, see our Selenium BiDi documentation.