上下文和信息:
我最近制作了一个简单的脚本,可以登录到外部网站并获取一些数据。该脚本的目的是获得学生成绩,然后将其转换为图表数据。为了简化获取数据的过程,我使用了npm库:selenium-webdriver。我使用该库而不是请求的原因(例如)是因为我需要登录,这样我才不会收到跨源错误(是的,因为我的服务器已经连接到前端,所以我会得到跨源错误。最终应用)。请注意,我所有的代码都在一个异步函数内部,并且像一个promise一样被调用(这意味着它是通过.then
而不是异步函数内部的await
来调用的。 )。
问题:
该脚本可以完美地为我提供所需的准确结果,但是仍然出现错误。这个错误使我感到困惑,原因有两个:首先,因为我所有的代码都在try块中,该try块中附加了catch并在其中调用了.catch()
。第二步,函数解决后,将记录错误。
错误消息:
(node:17908) UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id
(Driver info: chromedriver=73.0.3683.68
(47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17134 x86_64)
at Object.checkLegacyResponse (C:\Users\Redacted\Desktop\Application\Code\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (C:\Users\Redacted\Desktop\Application\Code\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (C:\Users\Redacted\Desktop\Application\Code\node_modules\selenium-webdriver\lib\http.js:468:26)
at processTicksAndRejections (internal/process/next_tick.js:81:5)
(node:17908) 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:17908) [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.
我的代码:
'use strict'
const {Builder, By, Key, until, Capabilities} = require('selenium-webdriver')
const Chrome = require('selenium-webdriver/chrome')
exports.simpleGradebookGetGrades = async function(username, password) {
const driver = new Builder().forBrowser('chrome').withCapabilities(Capabilities.chrome()).setChromeOptions(new Chrome.Options().addArguments('--remote-debugging-port=25470')).build()
try {
let retval = []
await driver.get('https://simplegradebook.ca/gradebook/login.php')
async function login(username, password) {
await driver.findElement(By.name('userid')).sendKeys(username)
await driver.findElement(By.name('password')).sendKeys(password)
await driver.findElement(By.name('login')).click()
await driver.wait(until.titleMatches(/.{20,}/))
return
}
await login(username, password)
for(let i of Object.keys(await driver.findElements(By.name('viewclasses')))) {
await driver.wait(until.elementsLocated(By.name('viewclasses')));
(await driver.findElements(By.name('viewclasses')))[i].click()
await driver.wait(until.elementLocated(By.tagName('tbody')))
retval.push(await driver.findElement(By.tagName('tbody')).getText())
await driver.get('https://simplegradebook.ca/gradebook/login.php')
login(username, password)
}
return retval
} catch(err) {
console.log(err)
} finally {
await driver.quit()
}
}
exports.simpleGradebookGetGrades('Redacted', 'Redacted').then(result => {
console.log(result)
}).catch(err => {
console.log(err)
})
我的问题:
为什么会发生错误?如何删除或忽略此错误?为什么我的catch块未捕获此错误?
其他信息:
节点版本:11.8.0
Selenium-webdriver版本:4.0.0-alpha.1
答案 0 :(得分:1)
如果使用以下Chrome浏览器选项来定义驱动程序,那么所有错误都会消失吗?
const chrome = require('selenium-webdriver/chrome')
const webdriver = require('selenium-webdriver')
let options = new chrome.Options()
let nextPort = 9222 //for example
options.addArguments(["--remote-debugging-port=" + nextPort])
let driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.setChromeOptions(options)
.build()