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.
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()
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.
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()