Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Puppeteer: line of code being executed before others

I have this code:

const puppeteer = require("puppeteer");

(async () => {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.goto("https://www.sisal.it/scommesse-matchpoint/quote/calcio/serie-a");

    const [button1] = await 
       page.$x('//div[@class="marketBar_changeMarketLabel__l0vzl"]/p');
    button1.click();

    const [button2] = await page.$x('//div[@class="listItem_container__2IdVR white 
       marketList_listItemHeight__1aiAJ marketList_bgColorGrey__VdrVK"]/p[text()="1X2 
       ESITO FINALE"]');
    button2.click();
})();

The proble is that after clicking button1 the page change and puppeteer executes immediately the following line of code, instead I want it to wait for the new page to be loaded becuase otherwise It will throw an error since It can't find button2.

I found this solution on stackoverflow:

const puppeteer = require("puppeteer");

function delay(time) {
    return new Promise(function (resolve) { 
        setTimeout(resolve, time);
    });
}

(async () => {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.goto("https://www.sisal.it/scommesse-matchpoint/quote/calcio/serie-a");

    const [button1] = await 
        page.$x('//div[@class="marketBar_changeMarketLabel__l0vzl"]/p');
    button1.click();

    await delay(4000);

    const [button2] = await page.$x('//div[@class="listItem_container__2IdVR white 
       marketList_listItemHeight__1aiAJ 
       marketList_bgColorGrey__VdrVK"]/p[text()="1X2 
       ESITO FINALE"]');
    button2.click();
})();

But of course this in't the best solution.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think you have to modify a bit in your code:

await button1.click();
await page.waitForNavigation({waitUntil: 'networkidle2'});

For reference, see the documentation.

about 4 years ago · Juan Pablo Isaza Report

0

I found a solution, here's the code:

const puppeteer = require("puppeteer");

(async () => {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.goto("https://www.sisal.it/scommesse 
        matchpoint/quote/calcio/serie-a");

    await page.waitForXPath('//div[@class="marketBar_changeMarketLabel__l0vzl"]/p');
    const [button1] = await page.$x('//div[@class="marketBar_changeMarketLabel__l0vzl"]/p');
    await button1.click();

    await page.waitForXPath('//div[@class="listItem_container__2IdVR white marketList_listItemHeight__1aiAJ marketList_bgColorGrey__VdrVK"]/p[text()="1X2 ESITO FINALE"]');
    const [button2] = await page.$x('//div[@class="listItem_container__2IdVR white marketList_listItemHeight__1aiAJ marketList_bgColorGrey__VdrVK"]/p[text()="1X2 ESITO FINALE"]');
    button2.click();
})();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!