selenium: [πŸ› Bug]: DevTools commands do not count as activity and session gets killed

What happened?

The selenium session gets killed (after 30 seconds) allthough I am sending a command via Chrome DevTools Protocol every second.

How can we reproduce the issue?

org/example/timeout.java:
----
package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;

public class timeout {
    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.setHeadless(true);

        WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"), options);

        driver = new Augmenter().augment(driver);

        driver.get("https://www.selenium.dev/documentation/grid/getting_started/");

        DevTools devTools = ((HasDevTools) driver).getDevTools();
        devTools.createSession();

        for (int i = 0; i < 360; i++) {
            System.out.print(".");

            devTools.send(org.openqa.selenium.devtools.v114.input.Input.dispatchMouseEvent(
                org.openqa.selenium.devtools.v114.input.Input.DispatchMouseEventType.MOUSEWHEEL,
                0,
                0,
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.of(0),
                Optional.of(100),
                Optional.empty()
            ));

            Thread.sleep(1_000);
        }
    }
}


pom.xml:
----
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Draft</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>4.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>4.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-devtools-v114</artifactId>
            <version>4.10.0</version>
        </dependency>
    </dependencies>
</project>


docker-compose.yml:
----
version: '3.7'

services:
  selenium:
    image: selenium/standalone-chrome:4.10.0
    environment:
      SE_NODE_GRID_URL: "http://localhost:4444"
      SE_NODE_SESSION_TIMEOUT: 30
    ports:
      - "4444:4444"

Relevant log output

Jun 19, 2023 12:03:42 PM org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer createTracer
INFO: Using OpenTelemetry for tracing
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
.................................Exception in thread "main" org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
Build info: version: '4.10.0', revision: 'c14d967899'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.90.1-microsoft-standard-WSL2', java.version: '19'
Driver info: driver.version: unknown
	at org.openqa.selenium.devtools.Connection.sendAndWait(Connection.java:183)
	at org.openqa.selenium.devtools.DevTools.send(DevTools.java:81)
	at org.example.timeout.main(timeout.java:31)
Caused by: java.util.concurrent.TimeoutException
	at java.base/java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1960)
	at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2095)
	at org.openqa.selenium.devtools.Connection.sendAndWait(Connection.java:172)
	... 2 more

Process finished with exit code 1

Operating System

Debian GNU/Linux 11 (bullseye)

Selenium version

Java 4.10.0

What are the browser(s) and version(s) where you see this issue?

Chrome 114.0.5735.106

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 114.0.5735.90

Are you using Selenium Grid?

4.10.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I implemented something that should work, I tested it a few times, and it went well.

This works for CDP and BiDi, the WebSocket messages when using VNC are ignored.

I think the right fix is to improve the ProxyNodeWebsockets, and every time something is sent over the socket, make the Node session cache aware that the session is being used.

This also is happening with Playwright if you are using Selenium Grid for its Chromium-based browser instantiation, which uses CDP/WS. From my Selenium Grid logs, the timeout is happening right around the default 5 minutes. I had to extend the session timeout to two hours to workaround.

As a workaround for Playwright, I first establish a web driver connection to Selenium Grid and then pass this web driver connection over to Playwright. To keep the web driver connection alive, it’s sufficient to call RemoteWebDriver.getCurrentUrl():

URL seleniumRemoteAddress = new URL("http://localhost:4444");

RemoteWebDriver remoteWebDriver = new RemoteWebDriver(seleniumRemoteAddress, new ChromeOptions());

Playwright.CreateOptions createOptions = new Playwright.CreateOptions()
    .setEnv(
        Map.of(
            "PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "true"
        )
    );

Playwright playwright = Playwright.create(createOptions);

BrowserType browserType = playwright.chromium();

String cdpAddress = "%s://%s:%s/session/%s/se/cdp".formatted(
    "https".equals(seleniumRemoteAddress.getProtocol()) ? "wss" : "ws",
    seleniumRemoteAddress.getHost(),
    seleniumRemoteAddress.getPort(),
    remoteWebDriver.getSessionId()
);

Browser browser = browserType.connectOverCDP(cdpAddress);

Page page = browser.newPage();

page.navigate("https://www.selenium.dev/documentation/grid/getting_started/");

for (int i = 0; i < 360; i++) {
    System.out.print(".");

    // Keep remote web driver alive
    remoteWebDriver.getCurrentUrl();

    page.mouse().wheel(0, 100);

    page.waitForTimeout(1000);
}