diff --git a/src/component/SeleniumBrowser.js b/src/component/SeleniumBrowser.js index 9258322..78a2174 100644 --- a/src/component/SeleniumBrowser.js +++ b/src/component/SeleniumBrowser.js @@ -2,6 +2,7 @@ const logger = require("logops") , { By, until } = require('selenium-webdriver') , captcha = require('../component/CaptchaComp') , poolingService = require('../service/PoolingService') +const { NoSuchSessionError } = require("selenium-webdriver/lib/error") class SeleniumBrowser { @@ -19,6 +20,12 @@ class SeleniumBrowser { let html = await driver.get(uri) .then(() => driver.findElement(By.css('body'))) .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) html = await p if (this.pool.isBorrowedResource(driver)) this.pool.release(driver) diff --git a/src/service/PoolingService.js b/src/service/PoolingService.js index 258d716..5d1f7f2 100644 --- a/src/service/PoolingService.js +++ b/src/service/PoolingService.js @@ -29,11 +29,10 @@ class PoolingService { } destroy(driver) { - logger.info('Quitting driver') - this.pool.release(driver) - return driver.quit().catch(err => { + logger.info('Quitting driver') + return this.pool.release(driver).then(() => driver.quit().catch(err => { logger.debug({ message: err.message }, 'Quitting error') - }) + })) } validate(driver) {