昨天使用微软自动化框架Playwright对浏览器进行全屏显示,多个浏览器调用等功能,今天我们来对浏览器进行前进/后退/刷新等操作。
直接上代码:
package org.example;import com.microsoft.playwright.*;public class BrowserFresh {
public static void main(String[] args) throws InterruptedException {
Playwright playwright = Playwright.create(); Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false)); BrowserContext browserContext = browser.newContext(new Browser.NewContextOptions()
.setIgnoreHTTPSErrors(true)
.setJavaScriptEnabled(true)
.setViewportSize(2880,1800)); Page page = browser.newPage(); page.navigate("https://www.163.com"); System.out.println("page title为 " + page.title()); Thread.sleep(3000); //打开另一个浏览器 page.navigate("https://www.baidu.com"); System.out.println("page title: " + page.title()); //前进操作 page.goForward(); Thread.sleep(3000); System.out.println("page title: " + page.title()); //刷新操作 page.reload(); Thread.sleep(3000); System.out.println("page title: " + page.title()); System.out.println("page url" + page.url()); browser.close(); playwright.close(); }
}
运行结果如下:
"C:\Program Files\Java\jdk1.8.0_271\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2023.1\lib\idea_rt.jar=56060:D:\Program Files\JetBrains\IntelliJ IDEA 2023.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_271\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_271\jre\lib\rt.jar;D:\code_liepin\playwrightDemo\target\classes;D:\code_liepin\apache-maven-3.8.1\repository\com\microsoft\playwright\playwright\1.31.0\playwright-1.31.0.jar;D:\code_liepin\apache-maven-3.8.1\repository\com\google\code\gson\gson\2.8.9\gson-2.8.9.jar;D:\code_liepin\apache-maven-3.8.1\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\code_liepin\apache-maven-3.8.1\repository\com\microsoft\playwright\driver\1.31.0\driver-1.31.0.jar;D:\code_liepin\apache-maven-3.8.1\repository\com\microsoft\playwright\driver-bundle\1.31.0\driver-bundle-1.31.0.jar;D:\code_liepin\apache-maven-3.8.1\repository\eu\bitwalker\UserAgentUtils\1.22-SNAPSHOT\UserAgentUtils-1.22-20190918.091221-1.jar;D:\code_liepin\apache-maven-3.8.1\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;D:\code_liepin\apache-maven-3.8.1\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;D:\code_liepin\apache-maven-3.8.1\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar" org.example.BrowserFresh
page title为 网易
page title: 百度一下,你就知道
page title: 百度一下,你就知道
page title: 百度一下,你就知道
page urlhttps://www.baidu.com/
Process finished with exit code 0
浏览器吊起了网易和百度
开始用的是page.pause();
发现浏览器直接就暂停不继续了,后来换成 Threed.sleep(3000);
完成了全部代码。
好的,今天就先到这里了
标签:Files,jdk1.8,Playwright,浏览器,jar,271,Java,Program,操作 From: https://blog.51cto.com/u_11894/6422865