当前位置:网站首页>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.

边栏推荐
- Elaborate on the operation of idea
- Playwirght深度入门
- TCP fastopen is used inside the origin server to quickly return to the source
- vs在连接SQL时出现的问题myconn.OPen();无法运行
- Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
- Eureka服务注册与发现
- 在线JSON转CSharp(C#)Class工具
- Chain tour airship development farmers' world chain tour development land chain tour development
- csrf攻击在laravel中如何解决
- leetcode210. Schedule II 207 Curriculum topology sorting DFS BFS
猜你喜欢

MIT CMS.300 Session 12 – IDENTITY CONSTRUCTION 虚拟世界中身份认同的建立 part 2

1278_FreeRTOS_借助prvAddCurrentTaskToDelayedList接口理解delayed task

Qt 使用QDomDocument读取xml文件

Hcip Road

MySQL (IV) - MySQL storage engine

WPS for thesis writing installs MathType plug-in to write mathematical formulas

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

MySQL (VIII) - explain

How to tag and label naming before the project release

HCIP之路第八次实验
随机推荐
The eighth experiment of hcip Road
浅析 Open API 设计规范
vs在连接SQL时出现的问题myconn.OPen();无法运行
20bn Jester complete dataset Download
. H5 file forgets the database name and uses h5py to print
在线JSON转CSharp(C#)Class工具
Which company would like to buy serious illness insurance in 2022?
MySQL (VIII) - explain
C WPF additional attribute implementation interface defines decorator
MySQL获取系统时间段
Product axure9 (English version), prototype design and production pull-down secondary menu
Yan's DP analysis
浅谈ThreadLocal和InheritableThreadLocal,源码解析
MySQL (II) - MySQL data type
Hcip Road
1.概率论-组合分析
一篇文章学会er图绘制
[pyqt5 series] modify the counter to realize control
Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
Detailed explanation of redis persistence, master-slave and sentry architecture