使用java无头浏览器创建web屏幕截图
我需要实现一个函数,为java后端项目中的网页制作屏幕截图。我发现一些方法,比如使用无头浏览器是一种很好的方法,但对于长页面或太多的图像,它们没有一种表现完美(如jbrowser和ashot)。我发现firefox有一个可以为我制作屏幕截图的功能。我想知道这个函数在headless模式下是否有java API?或者有没有其他方法可以获得更好的屏幕截图性能?非常感谢 这是我的截图代码使用java无头浏览器创建web屏幕截图,java,headless-browser,Java,Headless Browser,我需要实现一个函数,为java后端项目中的网页制作屏幕截图。我发现一些方法,比如使用无头浏览器是一种很好的方法,但对于长页面或太多的图像,它们没有一种表现完美(如jbrowser和ashot)。我发现firefox有一个可以为我制作屏幕截图的功能。我想知道这个函数在headless模式下是否有java API?或者有没有其他方法可以获得更好的屏幕截图性能?非常感谢 这是我的截图代码 package screenshot; import com.machinepublishers.jbrowse
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);
}
你说的性能是什么意思?代码截图需要很长时间吗?或者浏览器加载页面的速度太慢?请解释我的意思是,有时它会省略一些图像,有时它不能得到一个完整的页面,如果它太长。谢谢你的回答,我尝试了这种方法,但它不能得到整个页面的屏幕截图,需要滚动。另一个问题是,它严重依赖于chrome浏览器是否真的可以帮助您解决问题。TakesScreenshot也应该出现在firefox上。我可以看到上面的代码并使用它,但有时它仍然无法获取整个页面。您能告诉我们您要从中下载图像的url吗?您可以直接使用此stackoverflow页面的url