当前位置:网站首页>Playwirght getting started
Playwirght getting started
2022-06-23 07:45:00 【Luohanxiang】
Reference link :
Catalog
Two 、 know Playwright Inspector Gadget
3、 ... and 、 Open the web page 6 Walking
6、 Some students said , I want content
7、 Finally, there is a beginning and an end finally, close fall
One 、pom rely on
From what you know Playwright This word begins with , You can't live without it pom rely on , Look down :
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.20.0</version>
</dependency>Two 、 know Playwright Inspector Gadget
It can record your operation behavior , The generated code .
stay Java In the environment IDEA Medium Terminal Input in , You can open Playwright Inspector:
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen wikipedia.org"
( take “wikipedia.org” Change to a website that can be opened normally )
The first time you finish writing a program , If there is no relevant setting in the program , Will download palywright Built in browser binary file of the built-in browser , Need to wait for a while .
It is recommended to use mvn Command compilation , In this way, you can see the progress of the browser download
mvn compile exec:java -Dexec.mainClass="org.example.Example"( Never tried. )
3、 ... and 、 Open the web page 6 Walking
1、Playwright
Generate the default playwright Playwright playwright = Playwright.create()
or :
Generate additional related configurations Playwright
Playwright.CreateOptions createOptions = new Playwright.CreateOptions();
Map<String, String> map = new HashMap<>();
// Set ignore download browser , If the value is 0, Then download Playwright Default browser .( Blue Icon 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);
// Specify the browser type
chromeLaunchOptions.setChannel("chrome");
// Specify the execution directory of the browser
chromeLaunchOptions.setExecutablePath(Paths.get(new File(LocalChromePath).toURI()));
// Specify the timeout period
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();
or :
Page page = browserContext.newPage(); With page After the example :
// perform js
page.addInitScript("Object.defineProperties(navigator, {webdriver:{get:()=>undefined}});");// perform 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、 Open the web page
String targetUrl = "https://www.bbsmax.com"; page.navigate(targetUrl , new Page.NavigateOptions().setTimeout(120 * 1000));
6、 Some students said , I want content
// The content is coming String content = page.content();
// In the middle of the journey Playwright Inspector The window can be adjusted to facilitate debugging , The program will also stop here .
page.pause();
7、 Finally, there is a beginning and an end finally, close fall
BrowserContext context = page.context(); Browser browser = context.browser(); page.close(); context.close(); browser.close();
If your current BrowserContext、Browser 、Page The instance is in the current scope , Call the instance directly close The method can , There is no need to pass page.context() obtain BrowserContext.

边栏推荐
- 20bn Jester complete dataset Download
- 左乘右乘矩阵问题
- MySQL (II) - MySQL data type
- 快手350014
- Here comes the dry goods | PAAS collection to see first ~
- 传智教育 | 项目发布前如何打tag标签及标签命名规范
- U-Net: Convolutional Networks for Biomedical Image Segmentation
- 2. probability theory - axiom of probability theory
- MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts
- Spock sub piling
猜你喜欢

Online JSON to CSharp (c) class tool

How to tag and label naming before the project release

Eureka服务注册与发现

JS to determine the added and decreased elements of two arrays

vs在连接SQL时出现的问题myconn.OPen();无法运行

职场必备的30套报表模板,满足95%的报表需求,一键套用无需代码

Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列

Solutions to abnormal network connection of Xiaoai speakers

Playwirght深度入门

《一周的朋友》
随机推荐
279. perfect square
Several characteristics of MySQL database
The Sandbox 与《足球小将》达成合作,将流行的足球漫画及动画带入元宇宙
The original cloud landed in deep water, and the cloud product family of Boyun container released four values
User mode and kernel mode
Can you think of a better way to solve the problem of string inversion?
左乘右乘矩阵问题
MySQL (IV) - MySQL storage engine
职场必备的30套报表模板,满足95%的报表需求,一键套用无需代码
YGG Spain subdao Ola GG officially established
. H5 file forgets the database name and uses h5py to print
Decoding and practice of cmaf Technology
The eighth experiment of hcip Road
MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts
Both are hard disk partitions. What is the difference between C disk and D disk?
Online text filter less than specified length tool
What is the experience of being a data product manager in the financial industry
在线JSON转CSharp(C#)Class工具
Difference between char and varchar
Eureka服务注册与发现