英文:
Playwright => Not all the page loading
问题
以下是您提供的内容的中文翻译:
我正在使用 Playwright 编写一个 Python 脚本。
我想在获取相关快照之前等待页面的完全加载(例如 https://meteofrance.com/):
import asyncio
from playwright.async_api import async_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"))
如您所见,并非所有图像都已加载。
我尝试了一个超时延迟,await page.wait_for_load_state("networkidle")
,但没有起作用。
英文:
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.
答案1
得分: 1
The reason:
> 一些元素与延迟加载一起工作
因此,您需要模拟用户滚动。您可以使用mouse.wheel()或keyboard.press('PageDown')。以下只是一个示例:
# ...
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()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论