Wait Until the Element Disappears with Puppeteer (Pyppeteer)

Python
2021-06-09 13:24 (3 years ago) ytyng

Because it's Python, we're using Pyppeteer. The same approach should work for Puppeteer in JS as well.

Here, we introduce code to wait until an element disappears.

from pyppeteer.browser import Browser
from pyppeteer.errors import TimeoutError
from pyppeteer.page import Page


async def _wait_no_exist_for_selector(page: Page, selector, timeout=30000):
for i in range(int(timeout / 100)):
try:
await page.waitForSelector(selector, timeout=100)
await asyncio.sleep(0.1)
continue
except TimeoutError:
return

raise TimeoutError(f'{selector} did not disappear')

The timeout value for waitForSelector is the time used when the element doesn't exist, so it can be shorter (like 1).

The polling wait time when the element exists is the following:

await asyncio.sleep(0.1)

Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011