你可能在阅读旧版本的文档,最新版本的 page.screenshot() 方法已经支持 clip 参数了。以下是最新版本的文档中的 page.screenshot() 方法的描述:

async def screenshot(
    self,
    path: str,
    full_page: bool = False,
    quality: int = None,
    format: str = None,
    clip: Optional[Dict[str, Union[int, float]]] = None,
    omit_background: bool = False,
) -> bytes:
    '''Take a screenshot of the current page.

    :param path: The file path to save the image to. If `path` is a relative
        path, then it is resolved relative to the current working directory.
        If no path is provided, the image won't be saved to the disk.
    :param full_page: When true, takes a screenshot of the full scrollable
        page. Defaults to `False`, which captures only the visible viewport.
    :param quality: The quality of the image, between 0-100. Not applicable to
        png images.
    :param format: The image format. By default, the format is inferred from
        the file name extension. Supported extensions: `.png`, `.jpeg`, `.webp`.
    :param clip: An optional clip region to capture. Takes a dictionary with
        'x', 'y', 'width', and 'height' keys, all with integer values in pixels.
    :param omit_background: Hides default white background and allows capturing
        screenshots with transparency. Defaults to `False`.
    :returns: A bytes object representing the screenshot image.
    '''

你可以在 page.screenshot() 方法中传递 clip 参数,它是一个包含 'x', 'y', 'width''height' 键的字典,所有键都具有像素的整数值。这个参数可以用来截取屏幕上的特定区域,而不是整个屏幕。例如:

await page.screenshot(path='example.png', clip={'x': 0, 'y': 0, 'width': 800, 'height': 600})

这将截取位于 (0, 0) 点,宽度为 800,高度为 600 的矩形区域,并将其保存为 example.png 文件。

Python 中 Page.screenshot() 方法的 clip 参数使用指南

原文地址: https://www.cveoy.top/t/topic/oNjj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录