# Debug Selenium Tests

A failing Selenium test gives you a stack trace, such as a `NoSuchElementException` or a timed-out wait, but not what the page looked like, which request failed or what the browser console said at that moment. [WebdriverIO DevTools](/docs/devtools.md) records all of that for every WebDriver command of a Selenium test, in Node.js (Mocha, Jest, Cucumber or a plain script) and Python (pytest or a plain script). Use **live mode** to watch a failing test, inspect each step and rerun it on your machine, and **trace mode** to keep a replayable file of every failure in CI. You do not need to rewrite your tests or switch to WebdriverIO.

## Set up DevTools[​](#set-up-devtools "Direct link to Set up DevTools")

* Node.js
* Python

Install the adapter:

```
npm install @wdio/selenium-devtools --save-dev
```

Import it at the top of your test file. Under Mocha, Jest and Cucumber that one line is all the setup; the adapter finds the runner and its tests by itself.

test/login.test.js

```
import '@wdio/selenium-devtools'

import { strict as assert } from 'node:assert'

import { Builder, By, until } from 'selenium-webdriver'



describe('login', function () {

    let driver



    before(async function () {

        driver = await new Builder().forBrowser('chrome').build()

    })



    after(async function () {

        await driver?.quit()

    })



    it('logs in with valid credentials', async function () {

        await driver.get('https://the-internet.herokuapp.com/login')

        await driver.findElement(By.id('username')).sendKeys('tomsmith')

        await driver.findElement(By.id('password')).sendKeys('SuperSecretPassword!')

        await driver.findElement(By.css('button[type="submit"]')).click()

        const flash = await driver.wait(until.elementLocated(By.id('flash')), 10000)

        assert.match(await flash.getText(), /You logged into a secure area/)

    })

})
```

The adapter needs Node.js 22.23.4+, 24.22.1+ or 26.12.1+ and `selenium-webdriver` 4.8 or later. The Jest, Cucumber and plain-script setups are on the [Selenium DevTools](/docs/devtools/selenium.md#setup) page.

Install the adapter, then the dashboard backend once:

```
pip install selenium-devtools-py

selenium-devtools install-backend
```

Under pytest nothing goes in your test files. This is an ordinary Selenium test:

tests/test\_login.py

```
import pytest

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.ui import WebDriverWait





@pytest.fixture

def driver():

    driver = webdriver.Chrome()

    yield driver

    driver.quit()





def test_login(driver):

    driver.get("https://the-internet.herokuapp.com/login")

    driver.find_element(By.ID, "username").send_keys("tomsmith")

    driver.find_element(By.ID, "password").send_keys("SuperSecretPassword!")

    driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]').click()

    flash = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "flash")))

    assert "You logged into a secure area" in flash.text
```

The adapter needs Python 3.10+, `selenium>=4.44`, and Node.js 22.23.4+, 24.22.1+ or 26.12.1+ on `PATH`, because the dashboard backend is a Node application. The plain-script setup is on the [Selenium DevTools](/docs/devtools/selenium.md#setup) page.

## Debug a failing test on your machine[​](#debug-a-failing-test-on-your-machine "Direct link to Debug a failing test on your machine")

Run the test that fails:

* Node.js
* Python

```
npx mocha --timeout 60000 test/login.test.js
```

```
pytest --devtools tests/test_login.py
```

The DevTools dashboard opens in a new browser window and fills in as the test runs. When the run ends:

1. Select the failed test in the sidebar. The **Actions** list shows its WebDriver commands in the order they ran, and the one that failed is marked.
2. Select a command. The browser pane shows the page as it was after that command, so you can see whether the element you were waiting for was there, hidden or covered.
3. Use the bottom tabs to find the cause:

| You want to know                                               | Look at                                                               |
| -------------------------------------------------------------- | --------------------------------------------------------------------- |
| The error and the line that raised it                          | **Errors**, then **Source** to see the test code                      |
| What the selected command was called with and what it returned | **Log**                                                               |
| Whether an API call failed or returned something unexpected    | **Network**, then select a request for its headers, status and timing |
| Whether the app logged an error or threw an exception          | **Console**                                                           |

4. Fix the test or the app, then click ▶ on the test's row to rerun only that test. The rerun starts a fresh process, so it runs your edited code, and reports into the same window.

Assertions, from `node:assert` in Node.js and plain `assert` under pytest, are rows of their own carrying the expected and actual values, so a wrong value shows up in the list beside the commands that read it.

## Debug a test that fails only in CI[​](#debug-a-test-that-fails-only-in-ci "Direct link to Debug a test that fails only in CI")

CI has no screen to open a dashboard on, so record a trace instead, and keep only the tests that failed.

* Node.js
* Python

Configure the adapter once in a module your test files import, and switch to trace mode only in CI:

test/devtools.js

```
import { DevTools } from '@wdio/selenium-devtools'



if (process.env.CI) {

    DevTools.configure({

        mode: 'trace',

        traceGranularity: 'test',

        tracePolicy: 'retain-on-failure'

    })

}
```

In each test file, import that module in place of `@wdio/selenium-devtools`:

test/login.test.js

```
import './devtools.js'

import { Builder, By, until } from 'selenium-webdriver'

// ... the rest of the test as above
```

The module runs once however many test files import it, and before the first driver is built, which is when the adapter reads its options.

Pass the trace options on the CI command line. Naming a granularity or a policy turns trace mode on by itself:

```
pytest --devtools-trace-granularity test --devtools-trace-policy retain-on-failure tests/
```

Traces are written to a `test-results/` folder next to the test files. A passing test writes nothing, so a green run leaves nothing to upload. Upload the folders as a build artifact. On GitHub Actions:

.github/workflows/test.yml

```
      - name: Upload DevTools traces

        if: ${{ !cancelled() }}

        uses: actions/upload-artifact@v4

        with:

          name: devtools-traces

          path: |

            **/test-results/

            !**/node_modules/**

          if-no-files-found: ignore
```

To open the traces, download the artifact, unzip it and run:

* Node.js
* Python

```
npx show-trace test-results/
```

```
npx -p @wdio/devtools-backend show-trace test-results/
```

The [trace player](/docs/devtools/trace-player.md) lists every test with its outcome, failures first. It has the same tabs as the dashboard, plus DOM time-travel, the **A11y** tab and element overlay, which copy a working locator for any element the test touched, and a scrubbable filmstrip of the run. To open a trace without installing anything, drop the folder on [trace.webdriver.io](https://trace.webdriver.io); the file is read in your browser tab and uploaded nowhere.

Traces mask password fields and the `Authorization`, `Cookie` and `Set-Cookie` headers by default. Add [secrets](/docs/devtools/dashboard.md#secret-redaction) for other values, such as API tokens, before you share a trace: the `secrets` option in Node.js, `--devtools-secret-env <NAME>` under pytest.

## Debug a flaky test[​](#debug-a-flaky-test "Direct link to Debug a flaky test")

A flaky test fails on one run and passes on the next. Compare the two runs rather than reading two logs side by side.

On your machine, hover the failed test's row and click the bug-and-play icon next to ▶. [Preserve & Rerun](/docs/devtools/wdio/preserve-and-rerun.md) keeps the failed run, reruns the test, and opens the **Compare** tab with both runs aligned command by command, including their console and network. The first command where they differ is highlighted. This works for Node.js and Python alike.

In CI, retry failed tests and keep the trace of every test that needed a retry:

* Node.js
* Python

Change the policy in `test/devtools.js` from the previous section:

test/devtools.js

```
import { DevTools } from '@wdio/selenium-devtools'



if (process.env.CI) {

    DevTools.configure({

        mode: 'trace',

        traceGranularity: 'test',

        tracePolicy: 'retain-on-failure-and-retries'

    })

}
```

Then run Mocha with retries:

```
npx mocha --retries 1 --timeout 60000 test/
```

Each attempt writes its own folder, with a `-retry1` suffix on the retry, so you can open the failed attempt next to the one that passed. Retry-aware retention is verified with Mocha; the other policies are listed under [Retention](/docs/devtools/wdio/trace-mode.md#retention--tracepolicy).

The Python adapter does not know which attempt of a test it is recording, so the retry-aware policies behave like `retain-on-failure`: a test that failed and then passed on a retry is not kept. To catch a flake in CI, keep every test's trace on the runs where you hunt it:

```
pytest --devtools-trace-granularity test --devtools-trace-policy on tests/
```

## Hand a failure to a coding agent[​](#hand-a-failure-to-a-coding-agent "Direct link to Hand a failure to a coding agent")

Every trace contains `transcript.md`, a Markdown summary of the test's commands with their selectors, values and errors. Point your agent at the failed test's trace and ask it to read `transcript.md` first, or click **Copy** on the player's **Transcript** tab to paste the transcript and the failing errors into a chat.

## Troubleshooting[​](#troubleshooting "Direct link to Troubleshooting")

**`Chrome binary not found. Open manually: <url>`** (Node.js). The adapter could not find Chrome to open the dashboard window in. The run continues; open the printed URL in any browser.

**The dashboard window tries to open in CI.** Use trace mode in CI as shown above. To keep live mode without a window, set `openUi: false` in `DevTools.configure()` (Node.js) or `DEVTOOLS_OPEN=0` (Python).

**A Python run captures nothing.** Capture is opt-in: pass `--devtools` or `--devtools-trace`, or set `devtools = true` in your pytest configuration. Check that Node.js is on `PATH`, since the backend is needed in trace mode too, and run `selenium-devtools backend-path` to see whether the backend is installed.

**Two rows appear for one test.** `DevTools.startTest()` and `DevTools.endTest()` are only for plain scripts. Under Mocha, Jest or Cucumber the adapter already knows where each test starts and ends; remove the calls.

## Next steps[​](#next-steps "Direct link to Next steps")

* [Selenium DevTools](/docs/devtools/selenium.md): every option for Node.js and Python, and the Jest, Cucumber and plain-script setups.
* [Trace Mode](/docs/devtools/wdio/trace-mode.md): what a trace contains and how granularity and retention work.
* [Trace Player](/docs/devtools/trace-player.md): the player's tabs, keyboard shortcuts and shareable links.
* [Mobile Testing](/docs/devtools/mobile.md): debug Appium tests on Android and iOS.
* [Allure Integration](/docs/devtools/allure.md): attach traces to an Allure report.
