Search in sources :

Example 1 with LaunchOptions

use of com.ruiyun.jvppeteer.options.LaunchOptions in project epic4j by huisunan.

the class MainStart method getBrowser.

@Override
@SneakyThrows
public Browser getBrowser(String dataPath) {
    log.debug("driver data path :{}", dataPath);
    if (epicConfig.getNoSandbox()) {
        epicConfig.getDriverArgs().add("--no-sandbox");
    }
    // 自动下载,第一次下载后不会再下载
    if (Arrays.stream(Constant.EXECUTABLE_ENV).noneMatch(env -> {
        String chromeExecutable = System.getenv(env);
        return StrUtil.isNotBlank(chromeExecutable) && FileUtil.assertExecutable(chromeExecutable);
    })) {
        BrowserFetcher.downloadIfNotExist(epicConfig.getBrowserVersion());
    }
    LaunchOptions options = new LaunchOptionsBuilder().withArgs(epicConfig.getDriverArgs()).withHeadless(epicConfig.getHeadLess()).withUserDataDir(dataPath).withIgnoreDefaultArgs(Collections.singletonList("--enable-automation")).build();
    return Puppeteer.launch(options);
}
Also used : LaunchOptions(com.ruiyun.jvppeteer.options.LaunchOptions) LaunchOptionsBuilder(com.ruiyun.jvppeteer.options.LaunchOptionsBuilder) SneakyThrows(lombok.SneakyThrows)

Example 2 with LaunchOptions

use of com.ruiyun.jvppeteer.options.LaunchOptions in project epic4j by huisunan.

the class CrawTest method test.

@Test
@SneakyThrows
public void test() {
    BrowserFetcher.downloadIfNotExist("938248");
    String dataPath = new FileUrlResource("./data/chrome/tests").getFile().getAbsolutePath();
    LaunchOptions options = new LaunchOptionsBuilder().withArgs(Arrays.asList("--blink-settings=imagesEnabled=false", "--no-first-run", "--disable-gpu", "--no-default-browser-check", "--no-sandbox")).withHeadless(false).withUserDataDir(dataPath).withExecutablePath("").withIgnoreDefaultArgs(Collections.singletonList("--enable-automation")).build();
    Browser browser = Puppeteer.launch(options);
    Page page = browser.pages().get(0);
    // PageUtil.crawSet(page);
    // page.goTo("http://localhost:9999/");
    page.goTo("http://www.baidu.com/");
    TimeUnit.SECONDS.sleep(10);
    FileUrlResource errorDir = new FileUrlResource("data/test");
    File file = errorDir.getFile();
    log.debug("mkdir {}", file.mkdirs());
    ScreenshotOptions screenshotOptions = new ScreenshotOptions();
    screenshotOptions.setQuality(100);
    screenshotOptions.setPath(file.getAbsolutePath() + File.separator + "report.jpg");
    screenshotOptions.setType("jpeg");
    page.screenshot(screenshotOptions);
    FileUtil.writeUtf8String(page.content(), new File(file.getAbsolutePath() + File.separator + "report.html"));
    log.info("success");
}
Also used : LaunchOptions(com.ruiyun.jvppeteer.options.LaunchOptions) LaunchOptionsBuilder(com.ruiyun.jvppeteer.options.LaunchOptionsBuilder) ScreenshotOptions(com.ruiyun.jvppeteer.options.ScreenshotOptions) Page(com.ruiyun.jvppeteer.core.page.Page) FileUrlResource(org.springframework.core.io.FileUrlResource) File(java.io.File) Browser(com.ruiyun.jvppeteer.core.browser.Browser) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Aggregations

LaunchOptions (com.ruiyun.jvppeteer.options.LaunchOptions)2 LaunchOptionsBuilder (com.ruiyun.jvppeteer.options.LaunchOptionsBuilder)2 SneakyThrows (lombok.SneakyThrows)2 Browser (com.ruiyun.jvppeteer.core.browser.Browser)1 Page (com.ruiyun.jvppeteer.core.page.Page)1 ScreenshotOptions (com.ruiyun.jvppeteer.options.ScreenshotOptions)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1 FileUrlResource (org.springframework.core.io.FileUrlResource)1