Playwright => 并非所有页面加载

huangapple go评论86阅读模式
英文:

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"))

结果是:
https://ibb.co/r0xk00z

如您所见,并非所有图像都已加载。

我尝试了一个超时延迟,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()

huangapple
  • 本文由 发表于 2023年3月12日 19:43:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定