一、page
总览:
The Page class emits various events (described below) which can be handled using any of Node's native EventEmitter methods, such as on
, once
or removeListener
.
1、page加载时:
page.onLoad(p -> System.out.println("Page loaded!"));
2、取消默认事件加载(removeListener
):
案例:
Consumer<Request> logRequest = interceptedRequest -> {
System.out.println("A request was made: " + interceptedRequest.url());
};
page.onRequest(logRequest);
// Sometime later...
page.offRequest(logRequest);
3、page关闭时:
Page.onClose(handler)
4、Page.onConsoleMessage(handler)
Emitted when JavaScript within the page calls one of console API methods, e.g. console.log
or console.dir
. Also emitted if the page throws an error or a warning.
The arguments passed into console.log
appear as arguments on the event handler.
要点:获取到浏览器的js输出如log、dir,或者是抛出错误/警告,后执行page.onConsoleMessage里面的代码
案例:evaluate模拟打印后,触发onconsolemessage内部代码
page.onConsoleMessage(msg -> {
for (int i = 0; i < msg.args().size(); ++i)
System.out.println(i + ": " + msg.args().get(i).jsonValue());
});
page.evaluate("() => console.log('hello', 5, {foo: 'bar'})");
5、Page.onCrash(handler)
页面分配内存等导致奔溃后触发,后续操作终止,最好是捕获异常
try {
// Crash might happen during a click.
page.click("button");
// Or while waiting for an event.
page.waitForPopup(() -> {});
} catch (PlaywrightException e) {
// When the page crashes, exception message contains "crash".
}
6、Page.onDialog
Emitted when a JavaScript dialog appears, such as alert
, prompt
, confirm
or beforeunload
. Listener must either Dialog.accept([promptText]) or Dialog.dismiss() the dialog - otherwise the page will freeze waiting for the dialog, and actions like click will never finish.
要点:执行到如下代码时会一直等待js的dialog事件发生后才进行后续操作
page.onDialog(dialog -> {
dialog.accept();
});
7、Page.onDownload
触发下载操作后,执行代码块内语句
8、Page.onFileChooser(handler)
Emitted when a file chooser is supposed to appear, such as after clicking the <input type=file>
. Playwright can respond to it via setting the input files using FileChooser.setFiles(files[, options]) that can be uploaded after that.
要点:比如前端点击下载按钮后,触发选择路径下某个文件
page.onFileChooser(fileChooser -> {
fileChooser.setFiles(Paths.get("/tmp/myfile.pdf"));
});
9、goBack
Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If can not go back, returns null
.
Navigate to the previous page in history.
返回上一次page加载的页面,存在多个则返回最后一次
10、goForward
和goBack类似,不同的是goBack返回的是上一次page,而goForward可以选择
二、Locator
1、boundingBox
类似于通过页面元素的绑定框快捷操作元素,页面静态不发生滚动,则通过鼠标点击不影响
This method returns the bounding box of the element, or null
if the element is not visible. The bounding box is calculated relative to the main frame viewport - which is usually the same as the browser window.
Scrolling affects the returned bounding box, similarly to Element.getBoundingClientRect. That means x
and/or y
may be negative.
Elements from child frames return the bounding box relative to the main frame, unlike the Element.getBoundingClientRect.
Assuming the page is static, it is safe to use bounding box coordinates to perform input. For example, the following snippet should click the center of the element.
三、BrowserContext
1、setDefaultTimeout
相当于方法返回超时,超过设定时间后抛异常
注意:
Page.setDefaultNavigationTimeout(), Page.setDefaultTimeout() and BrowserContext.setDefaultNavigationTimeout() take priority over BrowserContext.setDefaultTimeout().