最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer: TimeoutError: waiting for selector - Stack Overflow

programmeradmin6浏览0评论

I am waiting for a selector to load on the page but I also need to handle cases where there is a timeout.

Currently, my script stops execution and does not continue. How can i handle error cases of timeout and still proceed with execution.

below is my relevant code.

    const newPagePromise = new Promise(res => browser.on('targetcreated', target => res(target.page())));
    for(const dataWorkSheet of dataWorkSheetsArray) {
       try{
         await page.evaluate(async () => {
          await $('.export--popup a').click();
         });
         const exportPopup = await newPagePromise;
         await Promise.all([
          await exportPopup.click('#data-0'),
          await exportPopup.waitForSelector('.cLink'),
         ]);
       } catch(e) {

       }
    }

How can i ensure, my loop continues even when there is a timeout error when executing waitForSelector?

I am waiting for a selector to load on the page but I also need to handle cases where there is a timeout.

Currently, my script stops execution and does not continue. How can i handle error cases of timeout and still proceed with execution.

below is my relevant code.

    const newPagePromise = new Promise(res => browser.on('targetcreated', target => res(target.page())));
    for(const dataWorkSheet of dataWorkSheetsArray) {
       try{
         await page.evaluate(async () => {
          await $('.export--popup a').click();
         });
         const exportPopup = await newPagePromise;
         await Promise.all([
          await exportPopup.click('#data-0'),
          await exportPopup.waitForSelector('.cLink'),
         ]);
       } catch(e) {

       }
    }

How can i ensure, my loop continues even when there is a timeout error when executing waitForSelector?

Share Improve this question asked Jun 23, 2020 at 14:24 opensource-developeropensource-developer 3,0786 gold badges48 silver badges109 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You could play with the catch of the promise:

await Promise.all([
  await exportPopup.click('#data-0'),
  await exportPopup.waitForSelector('.cLink').catch(error => console.log('failed to wait 
 for the selector'),
 ]);

Maybe you can considering add some more time to wait the element so that the script will continue to run?

await exportPopup.waitForSelector('.cLink', {timeout: 120000})
发布评论

评论列表(0)

  1. 暂无评论