[英]How to take full page screenshot using AShot library through Selenium and Java
I tried the below code for taking full page screenshot.我尝试了以下代码来获取整页屏幕截图。 But only the visible area is captured,
但只有可见区域被捕获,
public void Fullscreen (WebDriver driver)
{
try {
final Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
final BufferedImage image = screenshot.getImage();
ImageIO.write(image, "PNG", new File("D:\\" + "AShot_BBC_Entire.png"));
} catch(Exception e){
System.out.println(e.getMessage());
}
}
While working with Selenium Java Client v3.14.0 , ChromeDriver v2.41 , Chrome v 68.0 using ashot-1.4.4.jar here is an example to take the complete page screenshot both horizontally and vertically using ChromeDriver and aShot Library of the url https://jquery.com/
:在使用Selenium Java Client v3.14.0 、 ChromeDriver v2.41 、 Chrome v 68.0使用ashot-1.4.4.jar 时,这里是一个使用ChromeDriver和url
https://jquery.com/
aShot 库水平和垂直截取完整页面截图的示例https://jquery.com/
:
Code Block:代码块:
import java.io.File; import javax.imageio.ImageIO; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import ru.yandex.qatools.ashot.AShot; import ru.yandex.qatools.ashot.Screenshot; import ru.yandex.qatools.ashot.shooting.ShootingStrategies; public class ashot_CompletePage { public static void main(String[] args) throws Exception { System.setProperty("god.bless.you", "C:\\\\Utility\\\\BrowserDrivers\\\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); options.addArguments("disable-infobars"); options.addArguments("--disable-extensions"); WebDriver driver = new ChromeDriver(options); driver.get("https://jquery.com/"); new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery")); Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver); ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png")); driver.quit(); } }
Screenshots:截图:
You can find a detailed discussion in How to take screenshot with Selenium WebDriver您可以在How to take screenshot with Selenium WebDriver 中找到详细的讨论
Want to add answer in case when you don't know what kind of screen is used.如果您不知道使用哪种屏幕,想要添加答案。 (retina or not)
(视网膜与否)
In this case you need find devicePixelRatio of browser window:在这种情况下,您需要找到浏览器窗口的 devicePixelRatio:
Object output = ((JavascriptExecutor) webDriver).executeScript("return window.devicePixelRatio");
String value = String.valueOf(output);
float windowDPR = Float.parseFloat(value);
Then you can use ShootingStrategy with scaling;然后你可以使用 ShootingStrategy 和缩放;
ShootingStrategy shootingStrategy = ShootingStrategies.viewportPasting(ShootingStrategies.scaling(windowDPR), 100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.