Playwright => Not all the page loading


I’m writing a Python script using playwright.
I would like to wait the complete loading of a page (for example before taking the associated snapshot:

import asyncio
from playwright.async_api import async_playwright
from playwright.sync_api import Playwright, sync_playwright

async def take_screenshot(url,filename):
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True, args=["--disable-features=site-per-process"])
        page = await browser.new_page()
        await page.goto(url)
        await page.screenshot(path=filename, full_page=True)
        await browser.close()"","screenshot.png"))

The result is:

As you can see, not all the images are loaded.

I tried a timeout delay, await page.wait_for_load_state("networkidle") and nothing worked.

Asked By: deezer tsix



The reason:

Some elements work with lazy loading

So you need to emulate user scrolling. You can use mouse.wheel() or‘PageDown’). Here is just an example:

# ...
await page.goto(url)

for _ in range(7):
    await asyncio.sleep(0.5)

await page.screenshot(path=filename, full_page=True)
await browser.close()
Answered By: Danila Ganchar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.