24

Is there any way or Puppeteer API we can wait element to disappear or remove from Dom and then continue the execution?

E.g I have a loading animation I want to wait until this loading animation remove from Dom.

hardkoded
  • 18,915
  • 3
  • 52
  • 64
Mehran Shafqat
  • 422
  • 2
  • 5
  • 16

4 Answers4

35

waitForSelector has a hidden option which also check if the element is in the DOM:

await page.waitForSelector('div', {hidden: true});
hardkoded
  • 18,915
  • 3
  • 52
  • 64
12

Try this

await page.waitForFunction(() => !document.querySelector(querySelector));
Luke Taylor
  • 8,631
  • 8
  • 54
  • 92
dev_Fares
  • 151
  • 7
3

If you're waiting for API response, maybe it's better to rely on

await page.waitForResponse(response => response.url() === myUrl && response.status() === 200);
Dima Dorogonov
  • 2,297
  • 1
  • 20
  • 23
1

You can use page.waitForFunction with a conditional statement.

await page.waitForFunction('document.querySelector("#myElement") === null')

https://pptr.dev/#?product=Puppeteer&version=v5.2.1&show=api-pagewaitforfunctionpagefunction-options-args