Playwright官方文档要点记录(java)

一、page

总览:

        The Page class emits various events (described below) which can be handled using any of Node's native EventEmitter methods, such as ononce 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 alertpromptconfirm 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().

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值