ewrewr
This commit is contained in:
@@ -16,31 +16,41 @@ class SeleniumBrowser {
|
|||||||
this.sleep = m => new Promise(r => setTimeout(r, m))
|
this.sleep = m => new Promise(r => setTimeout(r, m))
|
||||||
this.pool = pool.createPool({
|
this.pool = pool.createPool({
|
||||||
create: () => {
|
create: () => {
|
||||||
|
logger.info('Create new driver instance')
|
||||||
return this.getDriver();
|
return this.getDriver();
|
||||||
},
|
},
|
||||||
destroy: function (driver) {
|
destroy: (driver) => {
|
||||||
driver.close()
|
logger.info('Quitting driver')
|
||||||
driver.quit()
|
return driver.quit()
|
||||||
|
},
|
||||||
|
validate: (driver) => {
|
||||||
|
return driver.getSession().then(s => {
|
||||||
|
logger.info('Driver session validation checked (id: %s)', s.getId())
|
||||||
|
return true
|
||||||
|
}).catch(err => {
|
||||||
|
this.pool.destroy(driver)
|
||||||
|
logger.debug({ message: err.message }, 'Driver session expired and destroy it')
|
||||||
|
return false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
max: 20, min: 1, evictionRunIntervalMillis: 1000 * 10,
|
max: 100, min: 1
|
||||||
acquireTimeoutMillis: 1000 * 5,
|
|
||||||
fifo: false,
|
|
||||||
softIdleTimeoutMillis: 1000 * 5
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.pool.on('factoryCreateError', function (err) {
|
this.pool.on('factoryCreateError', function (err) {
|
||||||
logger.error(err.message, 'pool error')
|
logger.error({ message: err.message }, 'pool error')
|
||||||
})
|
})
|
||||||
|
|
||||||
this.pool.on('factoryDestroyError', function (err) {
|
this.pool.on('factoryDestroyError', function (err) {
|
||||||
logger.error(err.message, 'pool error')
|
logger.error({ message: err.message }, 'pool error')
|
||||||
})
|
})
|
||||||
|
|
||||||
this.pool.use(async driver => {
|
this.pool.use(driver => {
|
||||||
let session = await driver.getSession()
|
return driver.getSession().then(s => driver).catch(err => {
|
||||||
return session && session.getId() ? driver : this.getDriver()
|
this.pool.destroy(driver)
|
||||||
}).then(/* a promise that will run after myTask resolves */)
|
throw new Error('Driver session expired pool should create new instance')
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
getDriver() {
|
getDriver() {
|
||||||
@@ -59,16 +69,12 @@ class SeleniumBrowser {
|
|||||||
logger.info('URL %s', uri)
|
logger.info('URL %s', uri)
|
||||||
if (uri.indexOf('://') > 3) {
|
if (uri.indexOf('://') > 3) {
|
||||||
return this.pool.acquire()
|
return this.pool.acquire()
|
||||||
// return this.getDriver()
|
|
||||||
.then(async driver => {
|
.then(async driver => {
|
||||||
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'))
|
||||||
let htmlPromise = this.isCaptchaPage(html) ? this.resolveCaptcha(driver) : this.getHtml(driver, html, params)
|
let htmlPromise = this.isCaptchaPage(html) ? this.resolveCaptcha(driver) : this.getHtml(driver, html, params)
|
||||||
html = await htmlPromise
|
html = await htmlPromise
|
||||||
driver.close()
|
|
||||||
driver.quit()
|
|
||||||
this.pool.destroy(driver)
|
|
||||||
return html
|
return html
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
59
test.js
Normal file
59
test.js
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
const logger = require("logops")
|
||||||
|
, { Builder, Capabilities, By, Key, until } = require('selenium-webdriver')
|
||||||
|
, url = require('url')
|
||||||
|
, fetch = require('node-fetch')
|
||||||
|
, pool = require('generic-pool')
|
||||||
|
, { Options } = require('selenium-webdriver/chrome');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.config = {
|
||||||
|
useHub: true, browser: 'chrome',
|
||||||
|
browserArgs: ['--no-sandbox', '--headless', '--disable-dev-shm-usage', '--proxy-server=spiduler-tor:8118']
|
||||||
|
}
|
||||||
|
this.capabilities = Capabilities.chrome()
|
||||||
|
|
||||||
|
this.getDriver().then(driver => {
|
||||||
|
driver.get('https://www.naver.com/')
|
||||||
|
.then(() => driver.findElement(By.css('body')))
|
||||||
|
.then(body => body.getAttribute('innerHTML'))
|
||||||
|
driver.getSession().then(s => {
|
||||||
|
let id = s.getId()
|
||||||
|
console.log(id)
|
||||||
|
// driver.close()
|
||||||
|
// driver.quit()
|
||||||
|
driver.get('https://www.naver.com/')
|
||||||
|
.then(() => driver.findElement(By.css('body')))
|
||||||
|
.then(body => body.getAttribute('innerHTML'))
|
||||||
|
.finally(() => {
|
||||||
|
// driver.close().then(a => console.log(a))
|
||||||
|
driver.quit().then(q => console.log(q))
|
||||||
|
})
|
||||||
|
// driver.quit()
|
||||||
|
return ''
|
||||||
|
}).catch(err => {
|
||||||
|
console.log(err.message)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async getDriver() {
|
||||||
|
let opts = new Options;
|
||||||
|
opts.addArguments(this.config.browserArgs)
|
||||||
|
return new Builder()
|
||||||
|
.usingServer('http://spiduler-chrome:4444/wd/hub')
|
||||||
|
.withCapabilities(this.capabilities)
|
||||||
|
.forBrowser(this.config.browser)
|
||||||
|
.setChromeOptions(opts)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const test = new Test
|
||||||
|
|
||||||
Reference in New Issue
Block a user