跳至主要内容

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

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

用法

Playwright.selectors()

返回值


webkit()

添加到:v1.9 之前 playwright.webkit()

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

用法

Playwright.webkit()

返回值