You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium-Webdriver返回无效堆栈追踪,无法定位测试错误位置

解决Selenium-WebDriver + Jasmine测试中堆栈追踪无法定位自身代码行的问题

使用selenium-webdriver结合Jasmine编写自动化测试时,测试代码出错后,Selenium返回的堆栈追踪无法定位到测试代码中具体哪一行(甚至哪个WebDriver命令)引发的错误,全是类似#0 0x55bb8653f303 <unknown>的无意义内容。

最小复现项目

package.json

{
  "name": "minimal-selenium-reproduction",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "test": "yarn jasmine --config=jasmine.json"
  },
  "devDependencies": {
    "chromedriver": "^121.0.0",
    "jasmine": "^5.1.0",
    "selenium-webdriver": "^4.18.1"
  }
}

jasmine.json

{
    "spec_dir": ".",
    "spec_files": [
      "**/*.spec.js"
    ],
    "env": {
      "stopSpecOnExpectationFailure": false,
      "random": false
    }
}

test.spec.js(故意编写错误代码)

const { Builder, By } = require("selenium-webdriver");

describe('selenium', function() {
    const driver = new Builder()
        .forBrowser('chrome')
        .build();

    it('should tell where an exception is thrown', async function () {
        await driver.get('https://duckduckgo.com');
        const searchField = await driver.findElement(By.css('#searchbox_input'));
        // 省略await后跳转页面,触发stale element reference错误
        searchField.sendKeys('how to get better stacktrace from selenium');
        await driver.get('https://wikipedia.org');
        await driver.close();
    })
})

执行yarn test后的错误输出

yarn run v1.22.21
$ yarn jasmine --config=jasmine.json
$ /home/walley/repos/minimal-selenium-reproduction/node_modules/.bin/jasmine --config=jasmine.json
Started
F

Failures:
1) selenium should tell where an exception is thrown
  Message:
    Unhandled promise rejection: StaleElementReferenceError: stale element reference: stale element not found
      (Session info: chrome=121.0.6167.139)
  Stack:
    error properties: Object({ remoteStacktrace: '#0 0x55bb8653f303 <unknown>
    #1 0x55bb862242b7 <unknown>
    #2 0x55bb86233c2c <unknown>
    #3 0x55bb8622a3da <unknown>
    #4 0x55bb86228659 <unknown>
    #5 0x55bb8622b8ef <unknown>
    #6 0x55bb8622b99c <unknown>
    #7 0x55bb862708fc <unknown>
    #8 0x55bb862709d3 <unknown>
    #9 0x55bb8626739e <unknown>
    #10 0x55bb86292382 <unknown>
    #11 0x55bb86263f88 <unknown>
    #12 0x55bb8629254e <unknown>
    #13 0x55bb862b107c <unknown>
    #14 0x55bb86292123 <unknown>
    #15 0x55bb86262095 <unknown>
    #16 0x55bb8626309e <unknown>
    #17 0x55bb865036ab <unknown>
    #18 0x55bb865074ba <unknown>
    #19 0x55bb864eff85 <unknown>
    #20 0x55bb8650812f <unknown>
    #21 0x55bb864d3e6f <unknown>
    #22 0x55bb8652c5f8 <unknown>
    #23 0x55bb8652c7c2 <unknown>
    #24 0x55bb8653e4a4 <unknown>
    #25 0x7fdc81733044 <unknown>
    ' })
        at Object.throwDecodedError (/home/walley/repos/minimal-selenium-reproduction/node_modules/selenium-webdriver/lib/error.js:521:15)
        at parseHttpResponse (/home/walley/repos/minimal-selenium-reproduction/node_modules/selenium-webdriver/lib/http.js:510:13)
        at Executor.execute (/home/walley/repos/minimal-selenium-reproduction/node_modules/selenium-webdriver/lib/http.js:443:28)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async thenableWebDriverProxy.execute (/home/walley/repos/minimal-selenium-reproduction/node_modules/selenium-webdriver/lib/webdriver.js:740:17)

1 spec, 1 failure
Finished in 4.323 seconds
error Command failed with exit code 3.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 3.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

解决方案

1. 确保所有WebDriver异步操作都加上await

测试代码中searchField.sendKeys()是异步方法,未加await会导致Promise未被正确处理,错误堆栈丢失上下文。修正后:

await searchField.sendKeys('how to get better stacktrace from selenium');

2. 启用Jasmine完整堆栈追踪

在jasmine.json的env字段中添加"fullStackTrace": true,保留更多错误上下文:

{
    "spec_dir": ".",
    "spec_files": [
      "**/*.spec.js"
    ],
    "env": {
      "stopSpecOnExpectationFailure": false,
      "random": false,
      "fullStackTrace": true
    }
}

3. 手动捕获并包装错误(可选)

为每个WebDriver操作添加错误捕获,自定义错误上下文,便于快速定位:

it('should tell where an exception is thrown', async function () {
    try {
        await driver.get('https://duckduckgo.com');
        const searchField = await driver.findElement(By.css('#searchbox_input'));
        await searchField.sendKeys('how to get better stacktrace from selenium');
        await driver.get('https://wikipedia.org');
        await driver.close();
    } catch (err) {
        throw new Error(`执行步骤出错: ${err.message}`, { cause: err });
    }
})

4. 保持依赖版本匹配

确保selenium-webdriver、chromedriver版本与Chrome浏览器版本一致,旧版本可能存在堆栈追踪的兼容性问题。

原因说明

  • 堆栈中的<unknown>行来自ChromeDriver的底层C++代码,不属于测试代码栈。
  • 未使用await处理异步操作会导致Promise静默拒绝,错误堆栈丢失测试代码的上下文关联。
  • Jasmine默认不显示完整的异步错误堆栈,启用fullStackTrace可保留更多定位信息。

内容的提问来源于stack exchange,提问作者Will Alley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 20:14:55