5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

puppeteerでclick遷移後に例外が発生する場合 Execution context was destroyed, most likely because of a navigation.

Posted at

puppeteerでpage.clickを実行して画面遷移後にpage.evalute等を実行するとUnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.のような例外が起こる場合がある。

これはpage.clickをawaitしてもページ遷移までは待っていないため、ページ遷移前に遷移前ページの要素にアクセスしてその最中にページ遷移が起こりアクセス中の要素が破壊されてしまい例外となっている。

そのため、page.clickをawaitするだけでなく、ページ遷移イベントもawaitしなければならない。

    await Promise.all([
        page.waitForNavigation({waitUntil: ['load', 'networkidle2']}),
        page.click('a.user-name')
    ]);

参考: https://qiita.com/hnw/items/a07e6b88d95d1656e02f

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?