相关疑难解决方法(0)

单击后如何让 Puppeteer waitForNavigation 工作

我试图让 puppeteer 在继续下一个语句之前等待导航完成。基于waitForNavigation()的文档,代码应该在下面工作。但它只是跳到下一个语句,我必须使用一种解决方法来等待响应中的特定 URL。

我也尝试了所有
waituntil选项(加载、domcontentloaded、networkidle0 和 networkidle2)。

任何如何让我正常工作的想法都值得赞赏。

const browser = await puppeteer.launch({
  headless: false,
})
const page = await browser.newPage()
const home = page.waitForNavigation()
await page.goto(loginUrl)
await home

const login = page.waitForNavigation()
await page.type('#email', config.get('login'))
await page.type('#password', config.get('password'))
await page.click('#submitButton')
await login // << skips over this

// the following line is my workaround and it works , but ideally I don't want 
// to specify the expected "after" page each time I navigate
await page.waitForResponse(request …
Run Code Online (Sandbox Code Playgroud)

node.js puppeteer

6
推荐指数
1
解决办法
7687
查看次数

在puppeteer中使用page.waitForNavigation的正确方法

我有点困惑,page.waitForNavigation我知道它是什么,它是做什么的,但是我根据互联网速度获得了不同的结果(这就是我认为的因素)

想象一下这段代码

    await page.$eval('#username', (el , setting ) => el.value = setting.username , setting );
    await page.$eval('#password', (el , setting ) => el.value = setting.password , setting );
    await page.$eval('form', form => form.submit());
    await page.waitForNavigation();
    await page.goto( 'http://example.com/dashboard'  );
Run Code Online (Sandbox Code Playgroud)

它填写登录表单,提交登录表单,等待表单提交,然后重定向到仪表板

这在我的本地主机上工作正常,我的互联网速度较慢(与服务器比较),但是当我将其上传到服务器时我得到了

Error: Navigation Timeout Exceeded: 30000ms exceeded 
Run Code Online (Sandbox Code Playgroud)

在服务器上,如果我await page.waitForNavigation();从代码中删除并重定向到仪表板,则可以正常工作

但是现在在localhost上,我可以重定向到仪表板,然后才能提交表单...所以我得到you cant see dashboard , your not logged in了这样的信息

我认为决定因素是互联网的速度

在服务器上,我的速度非常快,因此可以立即提交表单,并且await page.waitForNavigation()在行之前完成了表单,因此出现导航超时错误

但是在速度较慢的localhost上,需要更长的时间来提交表单,因此,我需要await page.waitForNavigation()在提交表单后才能拥有表单,否则我将被重定向到仪表板,然后表单才有机会提交

因此,即时通讯正在从有丰富经验的人那里寻求与puppeteer合作的建议,以解决这种情况……正确地知道,我在服务器或本地主机上运行时仍在编辑我的代码……这确实很烦人!

使用后

async function open_tab( setting ){ …
Run Code Online (Sandbox Code Playgroud)

javascript puppeteer

2
推荐指数
1
解决办法
3776
查看次数

标签 统计

puppeteer ×2

javascript ×1

node.js ×1