schickling/chromeless

Loop Through List of Webpages and Click a Button

Open

#361 opened on Dec 12, 2017

View on GitHub
 (0 comments) (0 reactions) (0 assignees)TypeScript (13,240 stars) (606 forks)batch import
help wantedquestion

Description

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))

Contributor guide