【问题标题】:Puppeteer: line of code being executed before othersPuppeteer:在其他人之前执行的代码行
【发布时间】:2021-10-12 08:18:23
【问题描述】:

我有这个代码:

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();
})();

问题是点击button1后页面发生变化,puppeteer立即执行以下代码行,而不是我希望它等待新页面加载,否则它会抛出错误,因为它找不到button2 .

我在 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();
})();

但这当然不是最好的解决方案。

【问题讨论】:

  • 我认为你需要await按钮点击。
  • @Phix 刚刚尝试过,但没有改变
  • 您可以等待页面导航:stackoverflow.com/questions/46948489/…。 $x 的行为也与page.waitForSelector() 相同吗?也许可以考虑使用该函数等到元素出现在页面上。
  • @Phix 它似乎只是有时起作用,当它不起作用时,脚本只是“冻结”并且永远不会点击 button2

标签: javascript node.js xpath puppeteer


【解决方案1】:

我认为您必须在代码中进行一些修改:

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

有关参考,请参阅documentation。

【讨论】:

  • 不幸的是它不起作用
【解决方案2】:

我找到了解决办法,代码如下:

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();
})();

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 2013-10-10
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多