如何使用 Puppeteer 等待事件触发的页面重新加载? [英] How to wait for event triggered page reloads with Puppeteer?

查看:271
本文介绍了如何使用 Puppeteer 等待事件触发的页面重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安全地处理在 select 元素上使用浏览器内事件触发页面重新加载 (POST) 的页面行为.URL 是相同的,但页面使用相同的 HTML 重新加载,唯一的区别是表格中内容的排序顺序.我尝试了几种方法,但不知何故都不可靠,我怎样才能实现这样的目标:

I'm trying safely handle the behavior of a page which uses an in-browser event on a select element to trigger a page reload (POST). The URL is the same but the page reloads with the same HTML and the only difference is the sort order of content in a table. I tried several methods but somehow none are reliable, how can I achieve something like this:

    try {
        await page.select('select[name=sort]', 'size'); 
        await page.waitForNextPageReload();
        await page.waitForSelector('select[name=sort]');
    } catch (error) {
        console.log('Error sorting page.');
    }

基本上,waitForNextPageReload 不存在,但我正在寻找可以提供类似结果的东西.我尝试添加延迟",但我正在寻找更可靠的方法来正确管理错误.

Basically, waitForNextPageReload doesn't exist but I'm looking for something which would provide similar results. I tried to add 'delays' but I'm looking for something more reliable to manage errors correctly.

推荐答案

选择和导航承诺之间可能存在竞争条件(参见示例 此处此处).你可以试试这个方法吗?

There may be a race condition between selecting and navigation promises (see examples here or here). Can you try this approach?

await Promise.all([
  page.select('select[name=sort]', 'size'),
  page.waitForNavigation(),
]);

await page.waitForSelector('select[name=sort]');

这篇关于如何使用 Puppeteer 等待事件触发的页面重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆