首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium Webdriver错误[UnhandledPromiseRejectionWarning]

Selenium Webdriver错误[UnhandledPromiseRejectionWarning]
EN

Stack Overflow用户
提问于 2019-11-16 11:45:40
回答 1查看 310关注 0票数 0

刚开始通过"Mozilla MDN web docs“进入selenium World,遇到了一个错误。请帮助我们继续前进。

正在尝试获取元素的文本属性,如p、title、alert...但它不能通过。

代码语言:javascript
复制
var webdriver = require('selenium-webdriver'),
	By = webdriver.By,
	until = webdriver.until;

var driver = new webdriver.Builder()
	.forBrowser('chrome')
	.build();

driver.get('http://mdn.github.io/learning-area/tools-testing/cross-browser-testing/accessibility/native-keyboard-accessibility.html');


var button = driver.findElement(By.css('button:nth-of-type(1)'));


button.click();


var alert = driver.switchTo().alert();

alert.getText().then(function(text) {
	console.log('Alert text is \'' + text + '\'');
})

alert.accept();

代码语言:javascript
复制
    uptimizators-MacBook-Air:study uptimizator$ node quick_test
(node:1941) UnhandledPromiseRejectionWarning: NoSuchAlertError: no such alert
  (Session info: chrome=78.0.3904.97)
    at Object.throwDecodedError (/Users/uptimizator/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/Users/uptimizator/node_modules/selenium-webdriver/lib/http.js:563:13)
    at Executor.execute (/Users/uptimizator/node_modules/selenium-webdriver/lib/http.js:489:26)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async thenableWebDriverProxy.execute (/Users/uptimizator/node_modules/selenium-webdriver/lib/webdriver.js:699:17)
(node:1941) 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: 1)
(node:1941) [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:1941) UnhandledPromiseRejectionWarning: NoSuchAlertError: no such alert
  (Session info: chrome=78.0.3904.97)
    at Object.throwDecodedError (/Users/uptimizator/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/Users/uptimizator/node_modules/selenium-webdriver/lib/http.js:563:13)
    at Executor.execute (/Users/uptimizator/node_modules/selenium-webdriver/lib/http.js:489:26)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async thenableWebDriverProxy.execute (/Users/uptimizator/node_modules/selenium-webdriver/lib/webdriver.js:699:17)
(node:1941) 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)
EN

回答 1

Stack Overflow用户

发布于 2019-11-16 13:04:05

几乎所有的东西都会返回一个承诺,所以你应该“等待”它们:

代码语言:javascript
复制
; (async function(){
  await driver.get('http://mdn.github.io/learning-area/tools-testing/cross-browser-testing/accessibility/native-keyboard-accessibility.html');

  var button = driver.findElement(By.css('button:nth-of-type(1)'));

  await button.click();

  var alert = driver.switchTo().alert();

  var text = await alert.getText()
  console.log('Alert text is \'' + text + '\'');

  await alert.accept()
})()

我不明白为什么文档是错误的,我猜它在旧的节点版本上是不同的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58887375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档