Wait Until the Element Disappears with Puppeteer (Pyppeteer)

Python
2021-06-09 04:24 (4 years ago)
Wait Until the Element Disappears with Puppeteer (Pyppeteer)

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)

Please rate this article
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive