Chrome Devtools Protocol with BiDi API
Usage
The following list of APIs will be growing as the Selenium project works through supporting real world use cases. If there is additional functionality you’d like to see, please raise a feature request.
As these examples are re-implemented with the WebDriver-Bidi protocol, they will be moved to the WebDriver Bidi pages.
Examples
Basic authentication
Some applications make use of browser authentication to secure pages. It used to be common to handle them in the URL, but browser stopped supporting this. With BiDi, you can now provide the credentials when necessary
Alternate implementations can be found at CDP Endpoint Basic Authentication and CDP API Basic Authentication
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
An alternate implementation may be found at CDP Endpoint Basic Authentication
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
const {Builder} = require('selenium-webdriver');
(async function example() {
try {
let driver = await new Builder()
.forBrowser('chrome')
.build();
const pageCdpConnection = await driver.createCDPConnection('page');
await driver.register('username', 'password', pageCdpConnection);
await driver.get('https://the-internet.herokuapp.com/basic_auth');
await driver.quit();
}catch (e){
console.log(e)
}
}())
val uriPredicate = Predicate { uri: URI ->
uri.host.contains("your-domain.com")
}
(driver as HasAuthentication).register(uriPredicate, UsernameAndPassword.of("admin", "password"))
driver.get("https://your-domain.com/login")
Pin scripts
This can be especially useful when executing on a remote server. For example, whenever you check the visibility of an element, or whenever you use the classic get attribute method, Selenium is sending the contents of a js file to the script execution endpoint. These files are each about 50kB, which adds up.
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
Mutation observation
Mutation Observation is the ability to capture events via WebDriver BiDi when there are DOM mutations on a specific element in the DOM.
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
const {Builder, until} = require('selenium-webdriver');
const assert = require("assert");
(async function example() {
try {
let driver = await new Builder()
.forBrowser('chrome')
.build();
const cdpConnection = await driver.createCDPConnection('page');
await driver.logMutationEvents(cdpConnection, event => {
assert.deepStrictEqual(event['attribute_name'], 'style');
assert.deepStrictEqual(event['current_value'], "");
assert.deepStrictEqual(event['old_value'], "display:none;");
});
await driver.get('dynamic.html');
await driver.findElement({id: 'reveal'}).click();
let revealed = driver.findElement({id: 'revealed'});
await driver.wait(until.elementIsVisible(revealed), 5000);
await driver.quit();
}catch (e){
console.log(e)
}
}())
Console logs and errors
Listen to the console.log events and register callbacks to process the event.
CDP API Console logs and WebDriver BiDi Console logs
Use the WebDriver BiDi Console logs implementation. HasLogEvents
will likely end up deprecated because it does not implement Closeable.
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
const {Builder} = require('selenium-webdriver');
(async () => {
try {
let driver = new Builder()
.forBrowser('chrome')
.build();
const cdpConnection = await driver.createCDPConnection('page');
await driver.onLogEvent(cdpConnection, function (event) {
console.log(event['args'][0]['value']);
});
await driver.executeScript('console.log("here")');
await driver.quit();
}catch (e){
console.log(e);
}
})()
fun kotlinConsoleLogExample() {
val driver = ChromeDriver()
val devTools = driver.devTools
devTools.createSession()
val logConsole = { c: ConsoleEvent -> print("Console log message is: " + c.messages)}
devTools.domains.events().addConsoleListener(logConsole)
driver.get("https://www.google.com")
val executor = driver as JavascriptExecutor
executor.executeScript("console.log('Hello World')")
val input = driver.findElement(By.name("q"))
input.sendKeys("Selenium 4")
input.sendKeys(Keys.RETURN)
driver.quit()
}
JavaScript exceptions
Listen to the JS Exceptions and register callbacks to process the exception details.
Use the WebDriver BiDi JavaScript exceptions implementation
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
Network Interception
Both requests and responses can be recorded or transformed.
Response information
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
Response transformation
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
const connection = await driver.createCDPConnection('page')
let url = fileServer.whereIs("/cheese")
let httpResponse = new HttpResponse(url)
httpResponse.addHeaders("Content-Type", "UTF-8")
httpResponse.body = "sausages"
await driver.onIntercept(connection, httpResponse, async function () {
let body = await driver.getPageSource()
assert.strictEqual(body.includes("sausages"), true, `Body contains: ${body}`)
})
driver.get(url)
val driver = ChromeDriver()
val interceptor = new NetworkInterceptor(
driver,
Route.matching(req -> true)
.to(() -> req -> new HttpResponse()
.setStatus(200)
.addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
.setContent(utf8String("Creamy, delicious cheese!"))))
driver.get(appServer.whereIs("/cheese"))
String source = driver.getPageSource()
Request interception
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs
SELENIUM_CI_TOKEN not set
If you want to render content fetched from the GitHub API, please create a
personal access token and set it as an environment variable named SELENIUM_CI_TOKEN. More information about token scopes can
be seen at
available scopes GitHub docs







