Skip to main content

Playwright

Playwright 模块提供了启动浏览器实例的方法。以下是使用 Playwright 驱动自动化的典型示例:

¥Playwright module provides a method to launch a browser instance. The following is a typical example of using Playwright to drive automation:

from playwright.sync_api import sync_playwright, Playwright

def run(playwright: Playwright):
chromium = playwright.chromium # or "firefox" or "webkit".
browser = chromium.launch()
page = browser.new_page()
page.goto("http://example.com")
# other actions...
browser.close()

with sync_playwright() as playwright:
run(playwright)

方法

¥Methods

stop

Added before v1.9 playwright.stop

如果 Playwright 实例是绕过 Python 上下文管理器创建的,则终止此实例。这在 REPL 应用中很有用。

¥Terminates this instance of Playwright in case it was created bypassing the Python context manager. This is useful in REPL applications.

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()

browser = playwright.chromium.launch()
page = browser.new_page()
page.goto("https://playwright.nodejs.cn/")
page.screenshot(path="example.png")
browser.close()

playwright.stop()

用法

¥Usage

playwright.stop()

返回

¥Returns


属性

¥Properties

chromium

Added before v1.9 playwright.chromium

该对象可用于启动或连接到 Chromium,返回 Browser 的实例。

¥This object can be used to launch or connect to Chromium, returning instances of Browser.

用法

¥Usage

playwright.chromium

类型

¥Type


devices

Added before v1.9 playwright.devices

返回与 browser.new_context()browser.new_page() 一起使用的设备字典。

¥Returns a dictionary of devices to be used with browser.new_context() or browser.new_page().

from playwright.sync_api import sync_playwright, Playwright

def run(playwright: Playwright):
webkit = playwright.webkit
iphone = playwright.devices["iPhone 6"]
browser = webkit.launch()
context = browser.new_context(**iphone)
page = context.new_page()
page.goto("http://example.com")
# other actions...
browser.close()

with sync_playwright() as playwright:
run(playwright)

用法

¥Usage

playwright.devices

类型

¥Type


firefox

Added before v1.9 playwright.firefox

该对象可用于启动或连接到 Firefox,返回 Browser 的实例。

¥This object can be used to launch or connect to Firefox, returning instances of Browser.

用法

¥Usage

playwright.firefox

类型

¥Type


request

Added in: v1.16 playwright.request

公开可用于 Web API 测试的 API。

¥Exposes API that can be used for the Web API testing.

用法

¥Usage

playwright.request

类型

¥Type


selectors

Added before v1.9 playwright.selectors

选择器可用于安装自定义选择器引擎。请参阅 extensibility 了解更多信息。

¥Selectors can be used to install custom selector engines. See extensibility for more information.

用法

¥Usage

playwright.selectors

类型

¥Type


webkit

Added before v1.9 playwright.webkit

该对象可用于启动或连接到 WebKit,返回 Browser 的实例。

¥This object can be used to launch or connect to WebKit, returning instances of Browser.

用法

¥Usage

playwright.webkit

类型

¥Type