简体   繁体   English

如何通过 Selenium 和 Java 使用 AShot 库截取整页截图

[英]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.0ChromeDriver v2.41Chrome v 68.0使用ashot-1.4.4.jar 时,这里是一个使用ChromeDriverurl 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:截图:

截屏


Reference参考

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.

相关问题 Selenium:无法使用 aShot 库截取完整的页面截图 - Selenium: Not able to take complete page screenshot using aShot library 如何使用Java Selenium Webdriver Ashot FireFox自动对网页进行全屏拍摄,包括固定元素 - How to automatically take full screen shot of a webpage include fixed element using java selenium webdriver ashot firefox 如何使用带有 Java 的 Selenium WebDriver 获取可滚动网页的整页屏幕截图? - How to take full page Screenshot of a scrollable webpage using Selenium WebDriver with Java? 如何使用 selenium java 进行彩色截图 - how to take color screenshot using selenium java 使用 Selenium 在 Chrome 中获取整页屏幕截图 - Take full page screenshot in Chrome with Selenium 每次与页面交互后如何截屏? Java, Selenium - How to take screenshot after every interaction with page? Java, Selenium 如何使用 Selenium 和 Java 获取完整网页的屏幕截图? - How to get screenshot of full webpage using Selenium and Java? 如何在firefox无头(java中的selenium)中截取屏幕截图? - How to take a screenshot in firefox headless (selenium in java)? 如何在 selenium 中使用 Ashot 为整个网页截屏 - How to use Ashot in selenium to take screen shot for entire webpage 如何在java上使用Selenium获取完整大小的页面快照? - How to take full size page snapshot with Selenium on java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM