diff --git a/example/src/main/java/com/ruiyun/example/Q_ScreenshotTest.java b/example/src/main/java/com/ruiyun/example/Q_ScreenshotTest.java index c4e0f13..1360fdd 100644 --- a/example/src/main/java/com/ruiyun/example/Q_ScreenshotTest.java +++ b/example/src/main/java/com/ruiyun/example/Q_ScreenshotTest.java @@ -3,14 +3,20 @@ import com.ruiyun.jvppeteer.api.core.Browser; import com.ruiyun.jvppeteer.api.core.ElementHandle; import com.ruiyun.jvppeteer.api.core.Page; +import com.ruiyun.jvppeteer.api.events.PageEvents; import com.ruiyun.jvppeteer.cdp.core.Puppeteer; +import com.ruiyun.jvppeteer.cdp.entities.ConsoleMessage; import com.ruiyun.jvppeteer.cdp.entities.ElementScreenshotOptions; import com.ruiyun.jvppeteer.cdp.entities.FrameAddStyleTagOptions; import com.ruiyun.jvppeteer.cdp.entities.GoToOptions; import com.ruiyun.jvppeteer.cdp.entities.ImageType; import com.ruiyun.jvppeteer.cdp.entities.ScreenshotOptions; +import com.ruiyun.jvppeteer.cdp.entities.WaitForOptions; +import com.ruiyun.jvppeteer.cdp.entities.WaitForSelectorOptions; import com.ruiyun.jvppeteer.common.PuppeteerLifeCycle; +import com.ruiyun.jvppeteer.util.Helper; import java.util.Collections; +import java.util.function.Consumer; import org.junit.Test; public class Q_ScreenshotTest extends A_LaunchTest { @@ -71,6 +77,7 @@ public void test5() throws Exception { page.screenshot(screenshotOptions); cdpBrowser.close(); } + //某个元素截图 @Test public void test6() throws Exception { @@ -130,7 +137,7 @@ public void test8() throws Exception { Page page = browser.newPage(); GoToOptions options = new GoToOptions(); options.setWaitUntil(Collections.singletonList(PuppeteerLifeCycle.networkIdle)); - page.goTo("https://www.baidu.com/s?wd=jvppeteer&rsv_spt=1&rsv_iqid=0x864033a90040d9e7&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=68018901_16_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=9&rsv_sug1=7&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=1778&rsv_sug4=1778",options); + page.goTo("https://www.baidu.com/s?wd=jvppeteer&rsv_spt=1&rsv_iqid=0x864033a90040d9e7&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=68018901_16_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=9&rsv_sug1=7&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=1778&rsv_sug4=1778", options); ElementScreenshotOptions screenshotOptions = new ElementScreenshotOptions(); screenshotOptions.setFromSurface(true); screenshotOptions.setPath("long.png"); @@ -138,4 +145,48 @@ public void test8() throws Exception { page.screenshot(screenshotOptions); browser.close(); } + + + //等待图片加载完毕 + @Test + public void test9() throws Exception { + launchOptions.setDevtools(true); + Browser browser = Puppeteer.launch(launchOptions); + //打开一个页面 + Page page = browser.newPage(); + page.on(PageEvents.Console, (Consumer) message -> System.out.println("console: "+message.text())); + WaitForOptions waitForOptions = new WaitForOptions(); + waitForOptions.setWaitUntil(Collections.singletonList(PuppeteerLifeCycle.networkIdle)); + + page.setContent(" \n" + + "\n" + + "\n" + + " \n" + + " \n" + + " Icon Home Example\n" + + "\n" + + "\n" + + "

Home Icon Example

\n" + + " \n" + + "\n" + + "", waitForOptions); + WaitForSelectorOptions options = new WaitForSelectorOptions(); + options.setVisible(true); + ElementHandle elementHandle = page.waitForSelector("#icon-home",options); + while (true){ + Object complete = elementHandle.evaluate("(element) => {\n" + + " return element.complete\n" + + "}"); + if((boolean)complete){ + break; + } + } + + ElementScreenshotOptions screenshotOptions = new ElementScreenshotOptions(); + screenshotOptions.setFromSurface(true); + screenshotOptions.setPath("图片.png"); + screenshotOptions.setFullPage(true); + page.screenshot(screenshotOptions); + browser.close(); + } }