首页 > 解决方案 > Puppeteer 不显示第一个循环周期的结果

问题描述

我有这段代码循环遍历一个页面,其中包含 3 个帧,并从中收集数据并将它们放在一起。问题是代码显示前 2 个循环的结果不完整,之后一切都很好,或者随机我得到一个像执行上下文这样的错误被破坏,很可能是因为导航。请原谅我的错误代码,但我只有 2 个月的时间来编写 javaScript 代码

const puppeteer = require('puppeteer');

const elementsToClickSelector = 'body > form > font > select  option';
const allLineIds = 'body > form > font > select > option';
const timpSosiri = 'body > b > font';


function run () {
    return new Promise(async (resolve, reject) => {
        try {
            const browser = await puppeteer.launch({ 
                headless: false
            });
            const page = await browser.newPage();

            await page.goto('');

            const frame =  page.frames().find(f => f.name() === 'stanga');
            const cframe =  page.frames().find(f => f.name() === 'centru');
            const dframe =  page.frames().find(f => f.name() === 'dreapta'); 

              // get all station name to be clicked
              let elementsToClick = await frame.$$(elementsToClickSelector);
              console.log(`Elements to click: ${elementsToClick.length}`);



              if (page.frames().find(f => f.name().includes('stanga'))) {
                  console.info('Frame  was in the DOM and in the frames list')
                } else {
                  console.error('Frame  was in the DOM but not in the frames list')
                }    

            let test =[]; 
            for (let i =0, length = elementsToClick.length; i< length; i++){
                const item = await frame.evaluateHandle((i) =>{
                    return document.querySelectorAll('option')[i];
                },i);
                await frame.waitFor(1000);
                const statieNume = await (await elementsToClick[i].getProperty('innerText')).jsonValue();
                console.log(statieNume);
                await item.click();

                // get all linie ids to be clicked
                let idLine = await cframe.$$(allLineIds);

                for(let j = 0, length1 = idLine.length; j<length1; j++){
                    const lineItem = await cframe.evaluateHandle((j) =>{
                        return document.querySelectorAll('option')[j];
                    }, j);

                 const linie = await (await idLine[j].getProperty('innerText')).jsonValue();
                 console.log(linie);   

                     lineItem.click();
                    cframe.waitForSelector('body > form > font > select option');


                    let timp = await dframe.$$(timpSosiri);
                   for( let k = 0, lengthk = timp.length; k < lengthk; k++){
                        const sosiri = await dframe.evaluateHandle((k) =>{
                            return document.querySelectorAll('b')[k];
                        },k);

                        dframe.waitForSelector('body > b > font');
                        sosiri.click();
                        const timpLinie = await (await timp[k].getProperty('innerHTML')).jsonValue();
                        console.log(timpLinie);

                        test.push({
                            statie:statieNume,
                            linie: linie,
                            timpi: timpLinie
                        });
                    }
                }

            }           
            browser.close();
            return resolve(JSON.stringify(test));
        } catch (e) {
            return reject(e);
        }
    })
}
run().then(console.log).catch(console.error);

输出

 Mures
    A Saguna
    [1] S5
    [0] E3
    [0] S5
    A.Guttenbrun_1
    [1] E8
    Sosire1: 17:31<br> Sosire2: 17:38
    [0] 21
    Sosire1: 17:31<br> Sosire2: 17:38
    A.Guttenbrun_2
    [0] S10
    Sosire1: 17:26<br> Sosire2: 17:55
    [1] Tv5
    Sosire1: 17:26<br> Sosire2: 17:55

First Mures doesn't display lines and time and A Saguna dosen't disply time.

标签: javascriptpuppeteer

解决方案


推荐阅读