意外的是const在eval $之前更改为null

时间:2018-12-28 15:46:49

标签: javascript node.js v8 puppeteer browser-automation

shelfCategoryLinks是指向特定类别的链接的数组, 尽管使用了await page.waitForSelector('.itemCount'); itemCounter,但有时返回null会破坏应用程序

 for (let shelf of shelfCategoryLinks) {
                logger.info('Processing shelf: ' + shelf.shelfName);

                //Wait some time before next operation
                await page.waitFor(400);

                //wait for submenu to appear
                await page.waitForSelector('#shelf-holder li.noChild');                    
                //Click into that submenu
                await page.click(`li[id='${shelf.shelfId}']`);
                //After click paroducts should load with also item counter which look like "Loaded 1-19 of 25 item"    
                //Wait for Item counter to appear
                await page.waitForSelector('.itemCount');
                //there we taking that item counter from DOM
                const itemCounter = await page.$('.itemCount');                    

                // Taking out total amount of items in shelf and parse to number from string
                const totalItem = await itemCounter.$eval(
                    'strong:last-child',
                    strong => parseInt(strong.innerHTML, 10)
                );
                console.log(totalItem);
            }

此代码按类别菜单移动,它应该从HTML返回一些数字,但是出人意料地在循环const itemCounter几圈之后返回HTML元素返回null,因为page.$eval导致崩溃的应用程序。为什么它返回NULL?

1 个答案:

答案 0 :(得分:0)

i添加  await page.reload(); 每次单击后都可以解决问题 我认为问题在于DOM不能正确地重新呈现(我的意思是某些节点正在使用新的节点进行切换)这导致waitForSelector didint必须等待,因此Pupeeteer单击了旧链接,或者试图在某些错误之前单击是