PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2022-08-14 21:08:35  修改时间:2024-11-28 02:37:25  分类:03.数据采集/爬虫  编辑
// 一些相关jar包
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.microsoft.playwright.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;



String url = "https://www.baidu.com";
        
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
	.setHeadless(false) // 取消无头模式,我们才能看见浏览器操作
	// .setChannel("chrome")
	.setArgs(ListUtil.of("--start-maximized"))
);

BrowserContext context = browser.newContext(new Browser.NewContextOptions().setViewportSize(null));

Page page = context.newPage();
page.navigate(url);

ThreadUtil.sleep(5, TimeUnit.SECONDS);
playwright.close();

 

参考:playwright 最大化启动

 


 

打开2个chromium窗口,第1个窗口打开2个标签页面,第2个窗口打开1个标签页面

String url = "https://www.baidu.com";
        
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
	.setHeadless(false) // 取消无头模式,我们才能看见浏览器操作
	// .setChannel("chrome")
	.setArgs(ListUtil.of("--start-maximized"))
);

BrowserContext context = browser.newContext(new Browser.NewContextOptions().setViewportSize(null));
Page page = context.newPage();
page.navigate(url);
Page page1 = context.newPage();
page1.navigate(url);

BrowserContext context2 = browser.newContext(new Browser.NewContextOptions().setViewportSize(null));
Page page2 = context2.newPage();
page2.navigate(url);


ThreadUtil.sleep(15, TimeUnit.SECONDS);
playwright.close();