首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用java中的无头浏览器创建网页截图

用java中的无头浏览器创建网页截图
EN

Stack Overflow用户
提问于 2019-05-07 19:59:07
回答 1查看 1.4K关注 0票数 2

我需要实现一个功能,使一个java后端项目的网页截图。我发现一些方法,如使用无头浏览器是一种很好的方法,但它们都不能完美地处理较长的页面或包含太多图像的情况(如jbrowser和ashot)。我发现火狐有一个功能可以为我截图。我想知道在headless模式下这个函数有没有java API?或者,有没有其他方法可以获得更好的截图性能?非常感谢

下面是我获取屏幕截图的代码

代码语言:javascript
复制
package screenshot;

import com.machinepublishers.jbrowserdriver.JBrowserDriver;
import com.machinepublishers.jbrowserdriver.Settings;
import com.machinepublishers.jbrowserdriver.Timezone;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;


import javax.imageio.ImageIO;
import java.io.*;


public class JbrowserTest {


    public String chekUrl(String str){
        if (str.startsWith("http://") || str.startsWith("https://")) {
            return str;
        }
return str;

    }
    public static void main(String[] args) throws UnsupportedEncodingException {

        // You can optionally pass a Settings object here,
        // constructed using Settings.Builder
        JBrowserDriver driver = new JBrowserDriver(Settings.builder().
                timezone(Timezone.ASIA_SHANGHAI).screen(new Dimension(1920,1080)).build());
        String url3 = "http://www.google.com";
        // This will block for the page load and any
        // associated AJAX requests
        driver.get(url3);
        driver.manage().window().maximize();
        // You can get status code unlike other Selenium drivers.
        // It blocks for AJAX requests and page loads after clicks
        // and keyboard events.
        System.out.println(driver.getStatusCode());
        // Returns the page source in its current state, including
        // any DOM updates that occurred after page load
        String string2 = new String(driver.getPageSource().getBytes("utf-8"),"gb2312");
        System.out.println(string2);
        Screenshot screenshot2 = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100))
                .takeScreenshot(driver);
        try {
            ImageIO.write(screenshot2.getImage(), "PNG",
                    new File("/Users/*******/Desktop/test2.png"));
            byte[] screenshot = driver.getScreenshotAs(OutputType.BYTES);
            System.out.println("the bytes" + screenshot.length);
            String filePath = "/Users/*******/Desktop/test.png";
            File file = new File(filePath);
            FileOutputStream fw = new FileOutputStream(file);
            fw.write(screenshot);
            fw.close();
        } catch (Exception ex) {
            System.out.println("error" + ex);
        }
        // Close the browser. Allows this thread to terminate.
        driver.quit();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-05-07 20:15:47

你没有确切地指定你的“性能需求”。使用selenium和chrome驱动程序截图的一种简单方法:

代码语言:javascript
复制
private void loadWebpage(){        
//Init driver
    ChromeOptions options = new ChromeOptions();
    options.setHeadless(true);

    options.addArguments("--window-size=1200x600", "--log-level=3");

    WebDriver driver = new ChromeDriver(options);

    //Load your website & wait until loaded with webdriver wait
     takeScreenShot(driver, new File("outputFile.png"))
}

public static void takeScreenshot(WebDriver driver, File screenshotFile) throws 
IOException {
    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    Files.copy(scrFile.toPath(), screenshotFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56022128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档