This commit is contained in:
brow3c
2021-05-15 23:53:56 +09:00
parent 9598d9b6fe
commit 586b0f15c6
4 changed files with 77 additions and 6 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')
, navigateService = require('../service/NavigateService')
class SeleniumBrowser { class SeleniumBrowser {
@@ -10,6 +11,10 @@ class SeleniumBrowser {
this.pool = poolingService.pool this.pool = poolingService.pool
} }
navigate(params) {
return navigateService.rowing(params)
}
async request(params) { async request(params) {
let uri = params.uri let uri = params.uri
logger.info('URL %s', uri) logger.info('URL %s', uri)

View File

@@ -9,7 +9,7 @@ class HeadlessService {
} }
get(params) { get(params) {
return browser.request(params) return params.hasOwnProperty('sessionId') ? browser.navigate(params) : browser.request(params)
} }
close() { close() {

View File

@@ -0,0 +1,60 @@
const logger = require("logops")
, { Builder, Capabilities, WebDriver, By } = require('selenium-webdriver')
, _http = require('selenium-webdriver/http')
, { Options } = require('selenium-webdriver/chrome');
class NavigateService {
constructor() {
this.config = {
browser: 'chrome',
hub: 'http://spiduler-chrome:4444/wd/hub',
browserArgs: ['--no-sandbox', '--headless', '--disable-dev-shm-usage']
}
}
rowing(params) {
return this[params.anchor](params)
}
login(params) {
this._getDriver(params.sessionId).then(driver => {
driver.get(params.uri).then(() => {
let username = driver.findElement(By.name(param.name.username))
let password = driver.findElement(By.name(param.name.password))
// let username = driver.findElement(By.xpath(param.xpath.username))
// let password = driver.findElement(By.xpath(param.xpath.password))
let submit = driver.findElement(By.xpath(param.xpath.submit))
username.sendKeys(params.key.username)
password.sendKeys(params.key.password)
return submit.click()
//*[@id="container_sub"]/div[2]/table/tbody/tr/td[1]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[3]/input
}).then(signed => {
return { currentUrl: driver.getCurrentUrl(), sessionId: driver.getSession() }
})
})
}
_getDriver(params) {
return params.sessionId ? this.getDriverExists(params) : this.getNewDriver()
}
_getDriverBySessionId(params) {
return new WebDriver(params.sessionId, new _http.Executor(Promise.resolve(this.config.hub)
.then(url => new _http.HttpClient(url, null, null))))
}
_getNewDriver() {
let capabilities = Capabilities.chrome()
let opts = new Options;
opts.addArguments(this.config.browserArgs)
return new Builder()
.usingServer(this.config.hub)
.withCapabilities(capabilities)
.forBrowser(this.config.browser)
.setChromeOptions(opts)
.build();
}
}
module.exports = new NavigateService

View File

@@ -1,6 +1,7 @@
const logger = require("logops") const logger = require("logops")
, config = require('../../config') , config = require('../../config')
, { Builder, Capabilities } = require('selenium-webdriver') , { Builder, Capabilities, WebDriver } = require('selenium-webdriver')
, _http = require('selenium-webdriver/http')
, pool = require('generic-pool') , pool = require('generic-pool')
, { Options } = require('selenium-webdriver/chrome'); , { Options } = require('selenium-webdriver/chrome');
@@ -57,6 +58,11 @@ class PoolingService {
}) })
} }
getDriverBySessionId(sessionId) {
new WebDriver(sessionId, new _http.Executor(Promise.resolve(url)
.then(url => new _http.HttpClient(url, null, null))))
}
getDriver() { getDriver() {
let opts = new Options; let opts = new Options;
let capabilities = Capabilities.chrome() let capabilities = Capabilities.chrome()