This commit is contained in:
spiduler
2020-10-20 08:12:48 +09:00
parent 645ea995d6
commit 7de1f460f0
3 changed files with 35 additions and 63 deletions

View File

@@ -6,57 +6,47 @@ const logger = require("logops")
, { Options } = require('selenium-webdriver/chrome');
class SeleniumBrowser {
constructor() {
this.driver = null
constructor() {
this.config = { useHub: true, browser: 'chrome' }
this.capabilities = Capabilities.chrome()
this.sleep = m => new Promise(r => setTimeout(r, m))
this.isRecaptchaReseponseEmpty = false
this.isRecaptchaReseponseEmpty = false
}
async setDriver() {
if (this.config.useHub) {
let opts = new Options;
opts.addArguments(this.getChromeArgs())
this.driver = await new Builder()
.usingServer('http://spiduler-chrome:4444/wd/hub')
.withCapabilities(this.capabilities)
.forBrowser(this.config.browser)
.setChromeOptions(opts)
.build();
} else {
this.driver = await this.setLocalDriver()
}
}
setLocalDriver() {
let opts = new Options
getDriver() {
let opts = new Options;
opts.addArguments(this.getChromeArgs())
return new Builder().forBrowser(this.config.browser).setChromeOptions(opts).build();
return new Builder()
.usingServer('http://spiduler-chrome:4444/wd/hub')
.withCapabilities(this.capabilities)
.forBrowser(this.config.browser)
.setChromeOptions(opts)
.build();
}
request(uri, waitElement) {
request(params) {
let uri = params.uri
let waitElement = params.waitElement
if (this.isRecaptchaReseponseEmpty) {
return ''
} else {
logger.info('URL %s', uri)
if (uri.indexOf('://') > 3) {
return this.driver.get(uri)
// .then(() => {
// if (waitElement) {
// return this.driver.wait(until.elementIsVisible(this.driver.findElement(By.css(waitElement))), 5 * 1000)
// }
// return true
// })
.then(() => this.driver.findElement(By.css('body')))
.then(body => body.getAttribute('innerHTML'))
.then(html => this.getHtml(html))
// .catch(error => {
// logger.error({ message: error.message }, 'No image element')
// return this.driver.findElement(By.css('body'))
// .then(body => body.getAttribute('innerHTML'))
// .then(html => this.getHtml(html))
// })
return this.getDriver().then(driver => {
return driver.get(uri)
.then(() => {
if (waitElement) {
return driver.wait(until.elementIsVisible(driver.findElement(By.css(waitElement))), 5 * 1000)
}
return true
})
.then(() => driver.findElement(By.css('body')))
.then(body => body.getAttribute('innerHTML'))
.then(html => this.getHtml(html))
.finally(() => {
driver.quit()
})
})
} else {
logger.info('URL %s is not valid', uri)
return ''
@@ -95,17 +85,11 @@ class SeleniumBrowser {
return res
}).then(res => {
logger.debug(res, '2captcha solved')
// this.driver.findElement(By.css('#g-recaptcha-response')).setAttribute('innerHTML', res.request)
this.driver.executeScript('captchaCallback("' + res.request + '")')
this.driver.wait(10000).then(d => {
this.driver.findElement(By.css('#g-recaptcha-response')).then(function (webElement) {
logger.error(res, 'Failed to overcome recaptcha')
}, function (err) {
// if (err.state && err.state === 'no such element') {
// console.log('Element not found');
// } else {
// webdriver.promise.rejected(err);
// }
logger.info('Success to overcome recaptcha')
})
})
@@ -131,7 +115,9 @@ class SeleniumBrowser {
}
async close() {
this.pool.drain().then(function () {
this.pool.clear();
});
}
getChromeArgs() {
@@ -152,5 +138,4 @@ class SeleniumBrowser {
}
const browser = new SeleniumBrowser
browser.setDriver()
module.exports = browser

View File

@@ -8,17 +8,14 @@ class HeadlessService {
}
get(uri, waitElement) {
return browser.request(uri, waitElement)
get(params) {
return browser.request(params)
}
close() {
return browser.close()
}
reset() {
browser.setDriver()
}
}
module.exports = new HeadlessService