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 实例,并将关闭所有仍在运行的已创建浏览器。
🌐 Terminates this instance of Playwright, will also close all created browsers if they are still running.
用法
Playwright.close();
create
Added in: v1.10启动新的 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);
参数
optionsPlaywright.CreateOptions(optional)
返回
属性
🌐 Properties
chromium()
Added before v1.9此对象可用于启动或连接到 Chromium,并返回 Browser 实例。
🌐 This object can be used to launch or connect to Chromium, returning instances of Browser.
用法
Playwright.chromium()
返回
firefox()
Added before v1.9此对象可用于启动或连接到 Firefox,并返回 Browser 实例。
🌐 This object can be used to launch or connect to Firefox, returning instances of Browser.
用法
Playwright.firefox()
返回
request()
Added in: v1.16公开可用于 Web API 测试的 API。
🌐 Exposes API that can be used for the Web API testing.
用法
Playwright.request()
返回
selectors()
Added before v1.9选择器可用于安装自定义选择器引擎。有关更多信息,请参阅可扩展性。
🌐 Selectors can be used to install custom selector engines. See extensibility for more information.
用法
Playwright.selectors()
返回
webkit()
Added before v1.9此对象可用于启动或连接到 WebKit,并返回 Browser 的实例。
🌐 This object can be used to launch or connect to WebKit, returning instances of Browser.
用法
Playwright.webkit()
返回