Playwright => Not all the page loading

Question:

I’m writing a Python script using playwright.
I would like to wait the complete loading of a page (for example https://meteofrance.com/) 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()

asyncio.run(take_screenshot("https://meteofrance.com/","screenshot.png"))

The result is:
https://ibb.co/r0xk00z

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

||

Answers:

The reason:

Some elements work with lazy loading

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

# ...
await page.goto(url)

for _ in range(7):
    await page.keyboard.press('PageDown')
    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.