跳到主要内容

Playwright

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

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

方法

close

添加于: v1.9 playwright.close

终止此 Playwright 实例,如果已创建的浏览器仍在运行,也将关闭它们。

用法

Playwright.close();

create

添加于: v1.10 playwright.create

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

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

用法

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

参数

  • options Playwright.CreateOptions (可选)
    • setEnv Map<String, String> (可选)添加于: v1.13#

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

返回值


属性

chromium()

早于 v1.9 添加 playwright.chromium()

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

用法

Playwright.chromium()

返回值


firefox()

早于 v1.9 添加 playwright.firefox()

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

用法

Playwright.firefox()

返回值


request()

添加于: v1.16 playwright.request()

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

用法

Playwright.request()

返回值


selectors()

早于 v1.9 添加 playwright.selectors()

Selectors 可用于安装自定义选择器引擎。有关更多信息,请参阅 extensibility

用法

Playwright.selectors()

返回值


webkit()

早于 v1.9 添加 playwright.webkit()

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

用法

Playwright.webkit()

返回值