`NoSuchSessionError:使用selenium时无效的会话ID`,即使应用程序工作正常

问题描述 投票:1回答:1

上下文和信息:

我最近做了一个简单的脚本,登录到外部网站并获取一些数据。这个脚本的目的是获得学生成绩,然后将其转换为可绘制的数据。为了使获取数据的过程更加容易,我使用了npm库:selenium-webdriver。我使用该库而不是请求(例如)的原因是因为我需要登录,因此我没有得到交叉原始错误(是的,我会得到交叉原因错误,因为我的服务器已连接到前端最终应用)。请注意,我的所有代码都在异步函数中,并且它被调用为promise(意味着它是使用.then调用的,而不是使用async函数内部的await调用)。

问题:

该脚本完美地为我提供了我想要的确切结果,但我仍然收到错误。这个错误让我感到困惑,因为它有两个方面:首先因为我的所有代码都在一个带有catch的try块中,并且在调用它时有一个.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

javascript node.js selenium selenium-webdriver web-scraping
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.