Playwright
第一个程序
public static void main(String[] args) {
Playwright playwright = Playwright.create();
// Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions();
launchOptions.setChannel("chrome");
launchOptions.setHeadless(false);
Browser browser = playwright.chromium().launch(launchOptions);
Page page = browser.newPage();
page.navigate("https://www.baidu.com/");
String title = page.title();
String url = page.url();
System.out.println("page title is : " + title);
System.out.println("page url is : " + url);
browser.close();
playwright.close();
}
自动捕捉
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="codegen https://www.baidu.com/"
debug
PWDEBUG=1
page.pause();
屏幕录制
package com.pihao.playwright;
import com.microsoft.playwright.*;
import java.nio.file.Paths;
public class TracerInspector {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
BrowserContext context = browser.newContext();
//开始录制
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true));
// Open new page
Page page = context.newPage();
// Go to https://www.bilibili.com/
page.navigate("https://www.bilibili.com/");
// Click text=登录
page.click("text=登录");
// Click [placeholder="请输入账号"]
page.click("[placeholder=\"请输入账号\"]");
// Fill [placeholder="请输入账号"]
page.fill("[placeholder=\"请输入账号\"]", "15083601111");
// Click [placeholder="请输入密码"]
page.click("[placeholder=\"请输入密码\"]");
// Fill [placeholder="请输入密码"]
page.fill("[placeholder=\"请输入密码\"]", "123456");
// Click text=注册 登录 >> div
page.click("text=注册 登录 >> div");
// Click div:has-text("输入手机号,注册账号")
page.click("div:has-text(\"输入手机号,注册账号\")");
// Fill [placeholder="请输入手机号"]
page.fill("[placeholder=\"请输入手机号\"]", "15083601111");
// Click [placeholder="请输入验证码"]
page.click("[placeholder=\"请输入验证码\"]");
// Fill [placeholder="请输入验证码"]
page.fill("[placeholder=\"请输入验证码\"]", "123456");
// Click text=登录/注册
page.click("text=登录/注册");
//结束录制
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));
}
}
}
观看录制
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="show-trace trace.zip"
或者使用这个网址
https://trace.playwright.dev/
打开多个窗口,只要重新new 一个
多个窗口
public static void main(String[] args) {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://www.baidu.com/");
System.out.println("page title is : " + page.title());
System.out.println("page url is : " + page.url());
BrowserContext context2 = browser.newContext();
Page page2 = context2.newPage();
page2.navigate("https://www.bilibili.com/");
System.out.println("page title is : " + page2.title());
System.out.println("page url is : " + page2.url());
}
Locators Concept
//single element
Locator locator = page.locator("text = 关于百度");
//如果有多个可以指定哪一个
Locator first = locator.first();
Locator last = locator.last();
first.hover();
first.click();
//multiple element
Locator locator = page.locator("selecst#id选择器的名字");
int count = locator.count();
for (int i = 0; i < count; i++) {
String textContent = locator.nth(i).textContent();
System.out.println(textContent);
}
//同样可以获取到下拉框中所有的值
List<String> strings = locator.allTextContents();
Text Selectors
Locator locator = page.locator("text = 关于百度");
//如果有多个可以指定哪一个
Locator first = locator.first();
Locator last = locator.last();
first.hover();
first.click();
//注意:如果没有text=的话,要使用单引号,不然获取不到内容
Locator locator1 = page.locator("text=Your Store");
Locator locator3 = page.locator("'Your Store'");
//选择h2标签,其中内容为New Customer的
Locator locator = page.locator("h2:has-text('New Customer')");
System.out.println(locator.textContent());
//选择class=header 的div标签下的h2标签,并且h2标签中的内容为New Customer
Locator locator2 = page.locator("div.header h2:has-text('New Customer')");
System.out.println(locator2.textContent());
//form表单登入
Locator locator4 = page.locator("from input:has-text('登入')");
Frame Selector
Handle ShadowDom Elements
Selecting Visible Elements
Inspect Playwright Selectors in Chrome Dev Tools
在自动捕捉的模式下,可以在控制台使用playwright 检查dom元素
注意:$ 是取多个元素, 是取多个元素, 是取多个元素,只取单个元素
Selecting elements that contain other elements
案例一
案例二
通过包含子的选择父的
Comma separated CSS Selectors and Xpath Union
Relative CSS Selectors in Playwright
left-of
right-of
above below
near
附近多少px元素
Nth Element Selector in Playwright
XPath Locator in Playwright
取第一个和最后一个
Complex Dynamic WebTable Handle
这里的scope是td标签中的属性
automatic login
Handle Javascript based popups
alert prompt confirm