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.

用法

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

用法

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

参数

  • options Playwright.CreateOptions (optional)
    • setEnv Map<String, String> (optional) Added in: v1.13#

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

返回


属性

🌐 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.

用法

Playwright.chromium()

返回


firefox()

Added before v1.9 playwright.firefox()

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

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

用法

Playwright.firefox()

返回


request()

Added in: v1.16 playwright.request()

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

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

用法

Playwright.request()

返回


selectors()

Added before v1.9 playwright.selectors()

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

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

用法

Playwright.selectors()

返回


webkit()

Added before v1.9 playwright.webkit()

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

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

用法

Playwright.webkit()

返回