schickling/chromeless

Loop Through List of Webpages and Click a Button

開放

#361 建立於 2017年12月12日

 (0 則留言) (0 個反應) (0 位負責人)TypeScript (606 個分叉)batch import
help wantedquestion

倉庫指標

星標
 (13,240 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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

貢獻者指南