puppeteer
- puppeteer 无头浏览器访问问卷星,模拟用户做页面操作
;(async () => {
const os = require('os')
const platform = os.platform()
const puppeteer = require('puppeteer')
const fs = require('fs')
const data = JSON.parse(fs.readFileSync('./data.json'))
const dataArr = Object.values(data)
function checktOs(str) {
return platform === str
}
await dataArr.reduce(async (pre, now) => {
await pre
return worker(now)
}, Promise.resolve())
async function worker(model) {
let arg = typeOs('win32') ? { headless: false, slowMo: 300 } : { args: ['--no-sandbox', '--disable-setuid-sandbox'] }
const browser = await puppeteer.launch(arg)
const page = await browser.newPage()
await page.goto(model.url)
await page.evaluate(model => {
document.querySelector('#q1').innerText = model.detail
document.querySelector('#q2').innerText = model.name
document.querySelector('#q3').innerText = model.num
model.ans.forEach((item, index) => {
if (!item) return
document.querySelector(`#divquestion${index} ul`).children[item - 1].click()
})
}, model)
const submit = await page.$('#submit_button')
await submit.click({ delay: 200 })
await browser.close()
return true
}
})()