刚刚开始通过“Mozilla MDN web docs”进入硒世界,并遇到了错误。请帮助我们继续前进。
尝试获取元素的文本属性,例如p、title、alert...但没有通过。
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();
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)
几乎所有事情都会返回一个承诺,所以你应该“等待”它们:
; (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()
})()
我不明白为什么文档是错误的,我猜它与旧节点版本的工作方式不同。