当前位置:网站首页>Playwirght getting started

Playwirght getting started

2022-06-23 07:45:00 Luohanxiang

Reference link :

Inspector | Playwright Java

Catalog

One 、pom rely on  

Two 、 know Playwright Inspector Gadget

3、 ... and 、 Open the web page 6 Walking

1、Playwright 

2、Browser

3、BrowserContext

4、Page  

5、 Open the web page

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.

原网站

版权声明
本文为[Luohanxiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230700543863.html