gpt4 book ai didi

java - 在java中通过 headless 浏览器创建网页截图

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:40 33 4
gpt4 key购买 nike

我需要在java后端项目中实现一个对网页进行截图的功能。我发现一些方法(例如使用 headless 浏览器)是一个好方法,但对于长页面或太多图像,它们都没有完美的性能(例如 jbrowser 和 ashot)。我发现firefox有一个功能可以帮我截屏。我想知道在 headless 模式下有没有这个函数的java API?或者有没有其他方法可以获得更好的截图性能?非常感谢

这是我获取屏幕截图的代码

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();
}
}

最佳答案

您没有准确指定您的“性能要求”。利用 selenium 和 chrome 驱动程序截取屏幕截图的简单方法:

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);
}

关于java - 在java中通过 headless 浏览器创建网页截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56022128/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com