当前位置:网站首页>Playwirght深度入门
Playwirght深度入门
2022-06-23 07:01:00 【罗汉翔】
参考链接:
目录
一、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"(没试过)
三、打开网页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。

边栏推荐
- Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family
- Online text filter less than specified length tool
- [interface automation] software testing the core skills of salary increase to increase salary by 200%
- Can you think of a better way to solve the problem of string inversion?
- g++编译命令使用
- The Sandbox 与《足球小将》达成合作,将流行的足球漫画及动画带入元宇宙
- Arthas thread command locates thread deadlock
- [Laoke] how should ordinary people learn technology?
- 电脑如何安装MySQL?
- 微信多人聊天及轮盘小游戏(websocket实现)
猜你喜欢

Simpledateformat thread safety issues

Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
three. Solution to stripe shadow and grid shadow in JS

How bootstrap clears floating styles

Hcip Road

Abnormal logic reasoning problem of Huawei software test written test

论文写作之WPS安装Mathtype插件编写数学公式

Akamai-1.75版本-_abck参数生成-js逆向分析

Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?

【博弈论】基础知识
随机推荐
MySQL (VIII) - explain
【AI实战】机器学习数据处理之数据归一化、标准化
Spock constraint - call frequency / target / method parameters
[Laoke] how should ordinary people learn technology?
Unity picture loading and saving
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
利用for循环输出一个字母三角形
论文写作之WPS安装Mathtype插件编写数学公式
Detailed explanation of redis persistence, master-slave and sentry architecture
csrf攻击在laravel中如何解决
Ldconfig command
[2022 graduation season] from graduation to transition to the workplace
How to quickly and gracefully download large files from Google cloud disk (II)
一篇文章学会er图绘制
Deploy kubersphere in kubernetes
小爱音箱连接网络异常解决办法
HCIP之路MPLS
[深度学习][原创]如何不用yolov5权重或者模型进行目标检测和绘制map等参数图
Tp6+redis+think-queue+supervisor implements the process resident message queue /job task
[game theory] basic knowledge