error catch for NoSuchSession

This commit is contained in:
spiduler
2021-01-15 22:55:36 +09:00
parent 5f985ec85f
commit 8c6616c8bd
2 changed files with 10 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ const logger = require("logops")
, { By, until } = require('selenium-webdriver') , { By, until } = require('selenium-webdriver')
, captcha = require('../component/CaptchaComp') , captcha = require('../component/CaptchaComp')
, poolingService = require('../service/PoolingService') , poolingService = require('../service/PoolingService')
const { NoSuchSessionError } = require("selenium-webdriver/lib/error")
class SeleniumBrowser { class SeleniumBrowser {
@@ -19,6 +20,12 @@ class SeleniumBrowser {
let html = await driver.get(uri) let html = await driver.get(uri)
.then(() => driver.findElement(By.css('body'))) .then(() => driver.findElement(By.css('body')))
.then(body => body.getAttribute('innerHTML')) .then(body => body.getAttribute('innerHTML'))
.catch(e => {
if (e instanceof NoSuchSessionError) {
this.pool.destroy(driver)
throw new NoSuchSessionError
}
})
let p = captcha.isCaptchaPage(html) ? captcha.resolveCaptcha(driver, this.pool) : this.getHtml(driver, html, params) let p = captcha.isCaptchaPage(html) ? captcha.resolveCaptcha(driver, this.pool) : this.getHtml(driver, html, params)
html = await p html = await p
if (this.pool.isBorrowedResource(driver)) this.pool.release(driver) if (this.pool.isBorrowedResource(driver)) this.pool.release(driver)

View File

@@ -29,11 +29,10 @@ class PoolingService {
} }
destroy(driver) { destroy(driver) {
logger.info('Quitting driver') logger.info('Quitting driver')
this.pool.release(driver) return this.pool.release(driver).then(() => driver.quit().catch(err => {
return driver.quit().catch(err => {
logger.debug({ message: err.message }, 'Quitting error') logger.debug({ message: err.message }, 'Quitting error')
}) }))
} }
validate(driver) { validate(driver) {