Javascript 木偶演员不';t显示第一个循环的结果

Javascript 木偶演员不';t显示第一个循环的结果,javascript,puppeteer,Javascript,Puppeteer,我有一段代码,它在一个页面上循环3帧,然后从中收集数据并将它们放在一起。问题是,代码在前2个循环中显示不完整的结果,之后一切正常,或者随机出现一个错误,比如执行上下文被破坏,很可能是因为导航。 请原谅我的错误代码,但我只有两个月的时间用javaScript编码 const puppeteer = require('puppeteer'); const elementsToClickSelector = 'body > form > font > select option'

我有一段代码,它在一个页面上循环3帧,然后从中收集数据并将它们放在一起。问题是,代码在前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);
const puppeter=require('puppeter');
常量元素StoclickSelector='body>form>font>select option';
const allLineIds='body>form>font>select>option';
const timposiri='body>b>font';
函数运行(){
返回新承诺(异步(解析、拒绝)=>{
试一试{
const browser=wait puppeter.launch({
无头:错
});
const page=wait browser.newPage();
等待第页转到(“”);
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');
//获取要单击的所有站点名称
让elementsToClick=wait frame.$$(elementsToClickSelector);
log(`elementsToClick:${elementsToClick.length}`);
if(page.frames().find(f=>f.name().includes('stanga')){
console.info('框架在DOM中并且在框架列表中')
}否则{
错误('帧在DOM中,但不在帧列表中')
}    
设test=[];
for(设i=0,length=element stoclick.length;i{
返回文档。querySelectorAll('option')[i];
},i);
等待帧。等待(1000);
const stationame=await(await元素单击[i].getProperty('innerText')).jsonValue();
console.log(stationum);
等待项。单击();
//获取要单击的所有linie ID
让idLine=wait cframe.$$(alllineid);
对于(设j=0,长度1=idLine.length;j{
返回文档.querySelectorAll('option')[j];
},j);
const linie=await(await idLine[j].getProperty('innerText')).jsonValue();
控制台日志(linie);
lineItem.click();
cframe.waitForSelector('body>form>font>select option');
设timp=等待数据帧$$(timposiri);
对于(设k=0,lengthk=timp.length;k{
返回文档。querySelectorAll('b')[k];
},k);
dframe.waitForSelector('body>b>font');
sosiri.click();
const timpLinie=await(await timp[k].getProperty('innerHTML')).jsonValue();
控制台日志(timpLinie);
测试推送({
statie:statienum,
琳妮:琳妮,
蒂姆皮:蒂姆普林尼
});
}
}
}           
browser.close();
返回解析(JSON.stringify(test));
}捕获(e){
退货拒收(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.
Mures
萨古纳
[1] S5
[0]E3
[0]S5
A.Guttenbrun_1
[1] E8
Sosire1:17:31
Sosire2:17:38 [0] 21 Sosire1:17:31
Sosire2:17:38 A.Guttenbrun_2 [0]S10 Sosire1:17:26
Sosire2:17:55 [1] Tv5 Sosire1:17:26
Sosire2:17:55 首先,墨尔斯不显示线条和时间,萨古纳·多森不显示时间。