【问题标题】:I got an error that is "UnhandledPromiseRejectionWarning: NoSuchSessionError: no such session"我收到一个错误“UnhandledPromiseRejectionWarning: NoSuchSessionError: no such session”
【发布时间】:2018-11-04 19:22:40
【问题描述】:

前提·我想实现的目标

我正在尝试使用 Selenium × Node.js 进行自动化测试,但它在调查阶段被阻止...

测试环境

  • Mac OS10.12.6
  • 节点 8.11.1
  • Selenium 服务器 3.12.0
  • Selenium WebDriver 4.0.0
  • ChromeDriver 2.38

我做了什么

1. Node.js的安装

From official site

2。 selenium-webdriver的安装

通过 npm 安装

$ npm install selenium-webdriver

3. selenium-server的安装

通过 Homebrew 安装

$ brew install selenium-server-standalone

4.安装 ChromeDriver

ChromeDriver - WebDriver for Chrome

→ 下载驱动后移动文件

$ mv /Users/username/Downloads/chromedriver ./
$ ls -la
total 31920 
drwxr-xr-x 7 username staff 238 5 24 17:42 . 
drwxr-xr-x+ 55 username staff 1870 5 24 17:42 .. 
-rwxr-xr-x@ 1 username staff 11917200 4 20 16:39 chromedriver 
drwxr-xr-x 41 username staff 1394 5 24 16:45 node_modules 
-rw-r--r-- 1 username staff 9367 5 24 16:45 package-lock.json

5.启动 Selenium 服务器

$ selenium-server -port 4444 & [1] 33415 C02SY1XFGTFJ:selenium username$ 18:04:14.512 INFO [GridLauncherV3.launch] - Selenium 构建信息:版本:'3.11.0',修订:'e59cfb3' 18:04:14.513 INFO [GridLauncherV3$1.launch] - 在端口 4444 上启动独立的 Selenium 服务器 2018-05-24 18:04:14.624:INFO::main: 记录初始化 @441ms 到 org.seleniumhq.jetty9.util.log.StdErrLog 18:04:14.882 信息 [SeleniumServer.boot] - 欢迎使用 Selenium for Workgroups.... 18:04:14.882 INFO [SeleniumServer.boot] - Selenium 服务器已启动并在端口 4444 上运行

出现的问题·错误信息

编写测试代码作为示例

$ vim sample.js

// Initialization of WebDriver
const webdriver = require('selenium-webdriver');
// Browser selection
const browser = new webdriver.Builder().forBrowser('chrome').build();

// Get page title
browser.get('http://example.selenium.jp/reserveApp/').then(()=>{
    browser.getTitle().then(title => console.log('Page title:',title))
});

// Exit the browser
browser.close();
browser.quit();

当你执行上面的代码时,...

$ node sample.js 
(node:13216) UnhandledPromiseRejectionWarning: NoSuchSessionError: no such session
  (Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.12.6 x86_64)
    at Object.checkLegacyResponse (/Users/ko-kamenashi/selenium/node_modules/selenium-webdriver/lib/error.js:585:15)
    at parseHttpResponse (/Users/ko-kamenashi/selenium/node_modules/selenium-webdriver/lib/http.js:533:13)
at Executor.execute (/Users/ko-kamenashi/selenium/node_modules/selenium-webdriver/lib/http.js:468:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:13216) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:13216) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我得到了错误。我该怎么办? 你能帮我一把吗?感谢您的阅读。

【问题讨论】:

    标签: node.js selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    错误在这里: 常量浏览器 = 新 webdriver.Builder().forBrowser('chrome').build();

    Best way: 
    (async ()=> { 
    await const browser = new webdriver.Builder().forBrowser('chrome').build();
    )();
    

    【讨论】:

    • 我也遇到了同样的错误,但这个解决方案对我不起作用。你能帮忙吗?
    • 您介意详细说明为什么这是答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多