初次使用 playwright ,代码记录

第一步:进入jar

<!--headless  -->
<dependency>
    <groupId>com.microsoft.playwright</groupId>
    <artifactId>playwright</artifactId>
    <version>1.17.0</version>
</dependency>

第二步:执行代码。

package com.bglemon.blue.taste;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

import java.nio.file.Paths;
class ApplicationTests {
    
     // 三个浏览器 截图
public static void main(String[] args) {
//        try (Playwright playwright = Playwright.create()) {
//            List<BrowserType> browserTypes = Arrays.asList(
//                    playwright.chromium(),
//                    playwright.webkit(),
//                    playwright.firefox()
//            );
//            for (BrowserType browserType : browserTypes) {
//                try (Browser browser = browserType.launch()) {
//                    BrowserContext context = browser.newContext();
//                    Page page = context.newPage();
//                    page.navigate("http://whatsmyuseragent.org/");
//                    page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("screenshot-" + browserType.name() + ".png")));
//                }
//            }
//        }
    // 单个浏览器 设置
    Browser browser = Playwright.create().chromium().launch();
    Browser.NewContextOptions options = new Browser.NewContextOptions();
    options.setViewportSize(700, 1344);
    BrowserContext context = browser.newContext(options);

    Page page = context.newPage();
    page.navigate("https://playwright.dev/");
    System.out.println("start");

    page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("screenshot1.png")));
    System.out.println("end");

    // 手机端页面
//        try (Playwright playwright = Playwright.create()) {
//            Browser browser = playwright.chromium().launch();
//            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)
//                    .setIsMobile(true)
//                    .setHasTouch(true)
//                    .setLocale("en-US")
//                    .setGeolocation(41.889938, 12.492507)
//                    .setPermissions(asList("geolocation")));
//            Page page = context.newPage();
//            page.navigate("https://www.openstreetmap.org/");
//            page.click("a[data-original-title=\"Show My Location\"]");
//            page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("colosseum-pixel2.png")));
//        }

    // 返回屏幕属性
//        try (Playwright playwright = Playwright.create()) {
//            Browser browser = playwright.firefox().launch();
//            BrowserContext context = browser.newContext();
//            Page page = context.newPage();
//            page.navigate("https://www.example.com/");
//            Object dimensions = page.evaluate("() => {\n" +
//                    "  return {\n" +
//                    "      width: document.documentElement.clientWidth,\n" +
//                    "      height: document.documentElement.clientHeight,\n" +
//                    "      deviceScaleFactor: window.devicePixelRatio\n" +
//                    "  }\n" +
//                    "}");
//            System.out.println(dimensions);
//        }
    //}


    // 登录大象模版
//        try (Playwright playwright = Playwright.create()) {
//            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
//                    .setHeadless(false));
//            BrowserContext context = browser.newContext(new Browser.NewContextOptions()
//                    .setAcceptDownloads(true));
//            // Open new page
//            Page page = context.newPage();
//            // Go to http://elephant-template.com/
//            page.navigate("http://elephant-template.com/");
//            // Go to http://elephant-template.com/#/
//            page.navigate("http://elephant-template.com/#/");
//            // Click text=登录
//            // page.waitForNavigation(new Page.WaitForNavigationOptions().setUrl("http://elephant-template.com/#/login"), () ->
//            page.waitForNavigation(() -> {
//                page.click("text=登录");
//            });
//            // Click [placeholder="请输入用户名"]
//            page.click("[placeholder=\"请输入用户名\"]");
//            // Fill [placeholder="请输入用户名"]
//            page.fill("[placeholder=\"请输入用户名\"]", "");
//            // Press CapsLock
//            page.press("[placeholder=\"请输入用户名\"]", "CapsLock");
//            // Fill [placeholder="请输入用户名"]
//            page.fill("[placeholder=\"请输入用户名\"]", "admin");
//            // Press Tab
//            page.press("[placeholder=\"请输入用户名\"]", "Tab");
//            // Fill [placeholder="请输入密码"]
//            page.fill("[placeholder=\"请输入密码\"]", "admin");
//            // Press Enter
//            // page.waitForNavigation(new Page.WaitForNavigationOptions().setUrl("http://elephant-template.com/#/"), () ->
//            page.waitForNavigation(() -> {
//                page.press("[placeholder=\"请输入密码\"]", "Enter");
//            });
//            // Click text=搜索 热门服务分类 > 通讯 / 社交 / 社区类产品 > 电商产品 > 文化产品 / 娱乐产品 > 安全 / 工具类产品 > 互联网+ > 游戏 > To B >> img
//            // page.waitForNavigation(new Page.WaitForNavigationOptions().setUrl("http://elephant-template.com/#/goodsList?searchData="), () ->
//            page.waitForNavigation(() -> {
//                page.click("text=搜索 热门服务分类 > 通讯 / 社交 / 社区类产品 > 电商产品 > 文化产品 / 娱乐产品 > 安全 / 工具类产品 > 互联网+ > 游戏 > To B >> img");
//            });
//            // Go to http://elephant-template.com/#/goodsDetail?productId=270
//            page.navigate("http://elephant-template.com/#/goodsDetail?productId=270");
//            // Click button:has-text("下载")
//            Download download = page.waitForDownload(() -> {
//                page.click("button:has-text(\"下载\")");
//            });
//            // assert page.url().equals("http://elephant-template.com/#/goodsDetail?productId=270");
//        }


    // 打开 页面操作
//        try (Playwright playwright = Playwright.create()) {
//            BrowserType chromium = playwright.chromium();
//            // Make sure to run headed.
//            Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false));
//            // Setup context however you like.
//            BrowserContext context = browser.newContext(/* pass any options */);
//          //  context.route("**/*", route -> route.resume());
//            context.route("http://elephant-template.com", route -> route.resume());
//            // Pause the page, and start recording manually.
//            Page page = context.newPage();
//            page.pause();
//        }


}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值