参考链接:
目录
一、pom依赖
从你认识Playwright这个单词起,你就离不开它的pom依赖,来往下看:
<dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.20.0</version> </dependency>
二、认识Playwright Inspector小工具
它可以记录你的操作行为,生成代码。
在Java环境下的IDEA中的Terminal中输入,即可打开Playwright Inspector:
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen wikipedia.org"
(将“wikipedia.org”换成能够正常打开的网站即可)
第一次写完程序运行时,如果没有在程序中进行相关设置,会下载palywright自带浏览器内置浏览器二进制文件,需要等待一会。
建议使用mvn命令编译,这样能看到浏览器下载进度
mvn compile exec:java -Dexec.mainClass="org.example.Example"
(没试过)
# Running without arguments will install default browsers
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="install"
三、打开网页6步走
1、Playwright
生成默认的的playwright Playwright playwright = Playwright.create()
或:
生成附加相关配置的Playwright Playwright.CreateOptions createOptions = new Playwright.CreateOptions(); Map<String, String> map = new HashMap<>(); // 设置忽略下载浏览器,如果值为0,则下载Playwright默认的浏览器。(蓝色图标的chrome) map.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "1"); createOptions.setEnv(map); Playwright playwright = Playwright.create(createOptions);
2、Browser
switch (name) { case "firefox": return playwright.firefox().launch(); case "chromium": BrowserType.LaunchOptions chromeLaunchOptions = new BrowserType.LaunchOptions(); chromeLaunchOptions.setHeadless(false); // 指定浏览器类型 chromeLaunchOptions.setChannel("chrome"); // 指定浏览器的执行目录 chromeLaunchOptions.setExecutablePath(Paths.get(new File(LocalChromePath).toURI())); // 指定超时时间 chromeLaunchOptions.setTimeout(120 * 1000); return playwright.chromium().launch(chromeLaunchOptions); case "webkit": return playwright.webkit().launch();
3、BrowserContext
if (isMobile) { BrowserContext context = browser.newContext(new Browser.NewContextOptions() .setUserAgent("Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36") .setViewportSize(411, 731) .setDeviceScaleFactor(2.625) .setLocale("zh-CN") .setIsMobile(true) .setHasTouch(true) .setLocale("en-US") .setGeolocation(41.889938, 12.492507) .setPermissions(Arrays.asList("geolocation"))); return context; } else { BrowserContext context = browser.newContext(new Browser.NewContextOptions() .setIgnoreHTTPSErrors(true) .setJavaScriptEnabled(true) .setViewportSize(1920, 1080) .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36")); return context; }
4、Page
Page page = browser.newPage();
或:
Page page = browserContext.newPage(); 有了page实例以后:
// 执行js page.addInitScript("Object.defineProperties(navigator, {webdriver:{get:()=>undefined}});");
// 执行js Object dimensions = page.evaluate("() => {\n" + " return {\n" + " width: document.documentElement.clientWidth,\n" + " height: document.documentElement.clientHeight,\n" + " deviceScaleFactor: window.devicePixelRatio\n" + " }\n" + "}"); // WindowDimensions:====>{width=1920, height=1080, deviceScaleFactor=1} System.out.println("WindowDimensions:====>" + dimensions); page.route("**", route -> { System.out.println("RouteUrl:====>" + route.request().url()); route.resume(); });
5、打开网页
String targetUrl = "https://www.bbsmax.com"; page.navigate(targetUrl , new Page.NavigateOptions().setTimeout(120 * 1000));
6、有同学说了,我要内容
// 内容这不就来了吗 String content = page.content();
// 中途可以把Playwright Inspector窗口调出来方便调试,程序也会停在这里。
page.pause();
7、最后有头有尾来个finally, close掉
BrowserContext context = page.context(); Browser browser = context.browser(); page.close(); context.close(); browser.close();
如果当前你的BrowserContext、Browser 、Page实例就在当前的作用域中,直接调用实例的close方法即可,不必通过page.context()获取BrowserContext。