简体   繁体   English

Puppeteer:Go 到页码

[英]Puppeteer: Go to page number

I'm facing a issue with a table and page number as link such as: 1, 2, 3, 4 etc… -> each number is a link.我面临一个表格和页码作为链接的问题,例如:1、2、3、4 等……-> 每个数字都是一个链接。

  • So i tried to creat a loop to click on different page number.所以我试图创建一个循环来点击不同的页码。
  • On the first interation, i'm able to reach the page 2在第一次交互时,我能够到达第 2 页
  • But on the second interation i got an error like below.但是在第二次交互时,我收到了如下错误。

Can you help me find out what's wrong?你能帮我找出问题所在吗?

(node:35692) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
    at rewriteError (/Users/admin/Downloads/Puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:262:23)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ExecutionContext._evaluateInternal (/Users/admin/Downloads/Puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:216:61)
    at async ExecutionContext.evaluate (/Users/admin/Downloads/Puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:107:16)
    at async ElementHandle.evaluate (/Users/admin/Downloads/Puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/JSHandle.js:102:16)
    at async ElementHandle._scrollIntoViewIfNeeded (/Users/admin/Downloads/Puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/JSHandle.js:280:23)
    at async ElementHandle.click (/Users/admin/Downloads/Puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/JSHandle.js:389:9)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:35692) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:35692) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Here is my code这是我的代码

const puppeteer = require('puppeteer');
const xlsx = require('xlsx');
const myUrl = 'http://www.ffbatiment.fr/federation-francaise-du-batiment/laffb/annuaire.html?Typ=1&Rs=&SecteurActivite=B%c3%a2timent&Dep=80&Acti=735&Activite=Menuiserie+%2f+Eb%c3%a9nisterie+%2f+Parquets'

const GetEntrepriseInfo = async page => {

    const result = await page.evaluate(() => {
        let name = document.querySelector('#ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_NomLabel').innerText;
        let mail = document.querySelector('#ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_EmailHyperLink').innerText;
        let cp = document.querySelector('#ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_CpLabel').innerText;
        let activite = document.querySelector('.SG_CContent_box_1').innerText;

        return { 
            name,
            mail,
            cp,
            activite
        };
    });

    return result;
};

const elementsToClickSelector = '.rgRow a';

(async () => {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.setViewport({
        width: 1000,
        height: 920
    });

    //go to url
    await page.goto(myUrl, { waitUntil:'networkidle2'});
    console.log("siteweb ok");
    await page.waitForTimeout(2000);

    // accept cookie
    await page.waitForSelector('#cookiescript_injected');
    page.click('#cookiescript_accept');
    console.log("cookie accepted");
    await page.waitForTimeout(2000);


    //submit the search
    await page.click('a.TypeRecherche');
    await page.waitForTimeout(2000);
    console.log("search submited");

    //next page
        // check how many page
        let pageNumber = await page.$$('thead .rgNumPart a');
        console.log(`Total pages: ${pageNumber.length}`);
        
        //next page iteration #2

        for (let z = 1; z < pageNumber.length; z++) {
            await page.waitForTimeout(5000);
            await page.waitForSelector('.Resultats');
            console.log('Current page :' + z);
            pageNumber[z].click();
            console.log('Page ' + z + ' has been clicked');
            await page.waitForTimeout(2000);
        }
        await page.waitForTimeout(5000);
        browser.close();
    })();

Clicking a link causes a navigation here, but you're not awaiting it anywhere.单击链接会导致此处导航,但您不会在任何地方等待它。 Moreover you aren't even awaiting page.click() .此外,您甚至都不在等待page.click() Try doing what the documentation says here :尝试按照文档中的说明进行操作:

await Promise.all([
    page.waitForNavigation(),
    pageNumber[z].click()
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM