schickling/chromeless

Loop Through List of Webpages and Click a Button

Open

#361 创建于 2017年12月12日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)TypeScript (13,240 star) (606 fork)batch import
help wantedquestion

描述

Hey I'm wondering why my code isn't working. I'm trying to loop through a list of URLs and click a button on each page and then wait for a success message. However, I'm getting a "not opened" error after the first one succeeds. Here's my code and the data2.csv is just a list of URLs:

const { Chromeless } = require('chromeless');
const LineByLineReader = require('line-by-line');

var fs = require('fs');
var util = require('util');
var logFile = fs.createWriteStream('log.txt', { flags: 'a' });
// Or 'w' to truncate the file every time the process starts.
var logStdout = process.stdout;

console.log = function () {
    logFile.write(util.format.apply(null, arguments) + '\n');
    logStdout.write(util.format.apply(null, arguments) + '\n');
}
console.error = console.log;

async function run() {
    const chromeless = new Chromeless();
    const lr = new LineByLineReader('dump2.csv');
    lr.on('line', function (line) {
        console.log(line);
        lr.pause();
        chromeless
            .goto(JSON.parse(JSON.stringify(line)))
            .evaluate(() => {
                console.log('hey');
            })
            .click('.loan-details li button')
            .wait('#alerts .alert')
            .end()
            .then(() => {
                lr.resume();
            })
            .catch((error) => {
                console.log(error);
                lr.resume();
            });
        // 'line' contains the current line without the trailing newline character.
    });

    lr.on('end', function () {
        chromeless.end()
        // All lines are read, file is closed now.
    });
}

run().catch(console.error.bind(console))

贡献者指南