我收到一个错误,“UnhandledPromiseRejectionWarning:NoSuchSessionError:没有这样的会话”

时间:2018-05-25 11:42:19

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

前提·我想要实现的目标

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

测试环境

  • Mac OS10.12.6
  • 节点8.11.1
  • Selenium Server 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 Server

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

我收到了错误。我该怎么办? 你能借给我一个手吗?谢谢你的阅读。

1 个答案:

答案 0 :(得分:1)

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

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