Skip to main content

GraiaX Playwright

适用于 Graia Project 的 Playwright 管理器

Code style: black Imports: isort License pdm-managed PyPI

Graiax Playwright 使用 launart 作为启动管理器, 适用于 Ariadne 及 Avilla。

通过 GraiaX Playwright 你可以轻松地在 Ariadne / Avilla 启动的时候同时启动一个 Playwright,并在其退出的时候自动关闭 Playwright。

需要注意的是,Playwright 将会在运行期间保持后台常驻,
但由于并未开启任何页面,其内存占用量不是非常大(但也是可观的)。

安装

pdm add graiax-playwright 或 poetry add graiax-playwright。

我们强烈建议使用包管理器或虚拟环境

开始使用

以下教程以配合 Launart 使用为例。

机器人入口文件

from creart import create
from launart import Launart
from graia.ariadne.app import Ariadne
from graiax.playwright import PlaywrightService

launart = create(Launart)
launart.add_component(PlaywrightService("chromium")) # 默认值为 chromium
launart.add_component(PlaywrightService("chromium", user_data_dir="./browser_data"))  # 与上一行二选一,该方式使用 Persistent Context
...

launart.launch_blocking()

配合 Graia Saya 使用

from creart import create
from launart import Launart
from graia.ariadne.util.saya import listen
from graiax.playwright import PlaywrightService


@listen(...)
async def function(app: Ariadne):
    launart = create(Launart)
    pw_service = launart.get_component(PlaywrightService)

    async with pw_service.page(  # 此 API 启用了自动上下文管理
        viewport={"width": 800, "height": 10},
        device_scale_factor=1.5,
    ) as page:
        await page.set_content("Hello World!")
        img = await page.screenshot(type="jpeg", quality=80, full_page=True, scale="device")
    ...

高级用法之一

上面配合 Saya 使用的例子展示了创建一个页面的例子,但该页面默认与其他页面互相隔离(例如 cookie 等),假如我们需要一个与其他页面隔离的新页面,那么我们可以使用 page(use_global_context=False) 在创建页面时使用一个新的上下文,如下所示:

@listen(...)
async def function(app: Ariadne):
    launart = create(Launart)
    pw_service = launart.get_component(PlaywrightService)
    async with pw_service.page(use_global_context=False) as page:  # 此 API 启用了自动上下文管理
        await page.set_content("Hello World!")
        img = await page.screenshot(type="jpeg", quality=80, full_page=True, scale="device")
    ...

高级用法之二

上面配合 Saya 使用的例子展示了为单个页面设置 viewport 的功能,自 GraiaX Playwright v0.3.1 版本起,可以在创建 PlaywrightService 时为全局的 Browser Context 指定 viewport,然后在截图时使用全局 Browser Context 截图,如下所示:

机器人入口文件:

launart.add_service(PlaywrightService("chromium"))

Saya 模块中:

from graiax.playwright import PlaywrightService


@listen(...)
async def function(app: Ariadne):
    launart = create(Launart)
    pw_service = manager.get_component(PlaywrightService)
    async with pw_service.context(...) as context:  # 此 API 启用了自动上下文管理
        page = context.new_page()
        try:
            await page.set_content("Hello World!")
            img = await page.screenshot(type="jpeg", quality=80, full_page=True, scale='device')
        finally:
            page.stop()
    ...

高级用法之三

通过依赖注入来获取 PlaywrightService,前面的代码中获取 PlaywrightService 都需要通过 Launart 的 get_component() 方法,你也可以通过自定义一个 BCC 的 Dispatcher 来实现依赖注入。

编写一个 Dispatcher:

from graia.broadcast.entities.dispatcher import BaseDispatcher
from graia.broadcast.interfaces.dispatcher import DispatcherInterface

class CustomDispatcher(BaseDispatcher):
    @classmethod
    async def catch(cls, interface: DispatcherInterface):
        with contextlib.suppress(TypeError):
            if generic_isinstance(interface.event, Service):
                manager = Launart.current()
                return manager.get_component(interface.annotation)

在启动 Launart 之前获取一个 BCC 实例并对其应用这个 Dispatcher:

from creart import it
from graia.broadcast import Broadcast

bcc = it(Broadcast)
bcc.finale_dispatchers.append(RedbotDispatcher)

...

launart.launch_blocking() # 或者是 avilla.launch()

之后用起来就很简单了,你可以对比一下有什么不同:

from graiax.playwright import PlaywrightService

@listen(...)
async def function(app: Ariadne, pw_service: PlaywrightService):
    async with pw_service.page(...) as page:
        ...

许可证

本项目使用 MIT 许可证进行许可。

Metadata

Release files for graiax-playwright 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graiax-playwright 0.5.0
File Size Uploaded Provenance Actions
graiax_playwright-0.5.0.tar.gz 16.3 kB With provenance Details for graiax_playwright-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for graiax-playwright 0.5.0
File Interpreter ABI Platform Actions
graiax_playwright-0.5.0-py3-none-any.whl Python 3 none any Details for graiax_playwright-0.5.0-py3-none-any.whl

Total release size: 33.9 kB

Release files / graiax_playwright-0.5.0.tar.gz

Download URL graiax_playwright-0.5.0.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9149183dfa4ae4917b4ebfb5eb7442b92d366c5fe0ea400b22552c363eb1ede2
BLAKE2b-256 checksum
How to use checksums
2b94a000ef4c61a24eef99a8f3cbae45ead3d4ace4bb15d276088307d6956b93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2025.

Transparency log

Release files / graiax_playwright-0.5.0-py3-none-any.whl

Download URL graiax_playwright-0.5.0-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b60668297717a9c26a9dbd32d98dae3df0f6a86217ea849141c1a4d802c218e
BLAKE2b-256 checksum
How to use checksums
589b92b0346ecc89fd6f7f1c3fb217a45991f28d714610afa3622c019bcdd68d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2025.

Transparency log

Security for graiax-playwright 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for graiax_playwright-0.5.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page