This commit is contained in:
Jay 2019-01-08 23:00:18 +08:00
parent fcc53ee3e7
commit 51cfd59350
3 changed files with 67 additions and 4 deletions

6
config.js Normal file
View File

@ -0,0 +1,6 @@
const env = process.env
module.exports = {
browser: env.BROWSER || '',
firefox_path: env.FIREFOX_PATH || '',
chrome_path: env.CHROME_PATH || ''
}

View File

@ -1,16 +1,38 @@
const { Builder, By, Key, until } = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome')
const firefox = require('selenium-webdriver/firefox')
const config = require('./config')
const setsUrl = 'https://www.echomtg.com/sets/'
const setsClass = '.main.marketAnalysis.sets'
if (!/^(firefox|chrome)$/i.test(config.browser)) {
console.log('browser type not support')
process.exit(1)
}
;(async () => {
let ffOpts = new firefox.Options()
ffOpts.headless()
ffOpts.setBinary('/root/apps/firefox/firefox')
let driver = await new Builder().forBrowser('firefox').setFirefoxOptions(ffOpts).build()
let opts = null
let driver = null
if (/firefox/i.test(config.browser)) {
opts = new firefox.Options()
opts.headless()
if (config.firefox_path.length > 0) {
opts.setBinary(config.firefox_path)
}
driver = await new Builder().forBrowser('firefox').setFirefoxOptions(opts).build()
} else if (/chrome/i.test(config.browser)) {
opts = new chrome.Options()
opts.headless()
if (config.chrome_path.length > 0) {
opts.setChromeBinaryPath(config.chrome_path)
}
driver = await new Builder().forBrowser('chrome').setChromeOptions(opts).build()
} else {
console.log('browser type not support')
}
// driver = await new Builder().forBrowser('firefox').setFirefoxOptions(ffOpts).build()
await driver.get(setsUrl)
let el = await driver.findElement(By.css(setsClass))
let sets = await el.findElements(By.css('h4>a'))

35
libs/driver.js Normal file
View File

@ -0,0 +1,35 @@
const { Builder, By, Key, until } = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome')
const firefox = require('selenium-webdriver/firefox')
const config = require('../config')
const setsUrl = 'https://www.echomtg.com/sets/'
const setsClass = '.main.marketAnalysis.sets'
module.exports.getDriver = async () => {
if (!/^(firefox|chrome)$/i.test(config.browser)) {
throw new Error('browser type not support')
}
let opts = null
let driver = null
if (/firefox/i.test(config.browser)) {
opts = new firefox.Options()
opts.headless()
if (config.firefox_path.length > 0) {
opts.setBinary(config.firefox_path)
}
driver = await new Builder().forBrowser('firefox').setFirefoxOptions(opts).build()
} else if (/chrome/i.test(config.browser)) {
opts = new chrome.Options()
opts.headless()
if (config.chrome_path.length > 0) {
opts.setChromeBinaryPath(config.chrome_path)
}
driver = await new Builder().forBrowser('chrome').setChromeOptions(opts).build()
} else {
throw new Error('browser type not support')
}
return driver
}