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:

import com.microsoft.playwright.*;

public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
Browser browser = chromium.launch();
Page page = browser.newPage();
page.navigate("http://example.com");
// other actions...
browser.close();
}
}
}

方法

¥Methods

close

Added in: v1.9 playwright.close

终止此 Playwright 实例,并将关闭所有仍在运行的已创建浏览器。

¥Terminates this instance of Playwright, will also close all created browsers if they are still running.

用法

¥Usage

Playwright.close();

create

Added in: v1.10 playwright.create

启动新的 Playwright 驱动程序进程并连接到它。当不再需要实例时,应调用 Playwright.close()

¥Launches new Playwright driver process and connects to it. Playwright.close() should be called when the instance is no longer needed.

Playwright playwright = Playwright.create();
Browser browser = playwright.webkit().launch();
Page page = browser.newPage();
page.navigate("https://www.w3.org/");
playwright.close();

用法

¥Usage

Playwright.create();
Playwright.create(options);

参数

¥Arguments

  • options Playwright.CreateOptions(可选)

    ¥options Playwright.CreateOptions (optional)

将传递给驱动程序进程的其他环境变量。默认情况下,驱动程序进程会继承 Playwright 进程的环境变量。

¥Additional environment variables that will be passed to the driver process. By default driver process inherits environment variables of the Playwright process.

返回

¥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()

返回

¥Returns


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

返回

¥Returns


request()

Added in: v1.16 playwright.request()

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

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

用法

¥Usage

Playwright.request()

返回

¥Returns


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

返回

¥Returns


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

返回

¥Returns