【问题标题】:Selenium: Not able to take complete page screenshot using aShot librarySelenium:无法使用 aShot 库获取完整的页面截图
【发布时间】:2018-11-25 01:45:23
【问题描述】:

我正在尝试使用 Firefox gecko 驱动程序和 aShot Library 在水平和垂直方向上截取完整的页面截图。

但是,结果并不如预期。看看:

driver.get("https://google.com");

Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"JPEG",new File("FullPageScreenshot.jpg"));

查看了很多变体,但没有任何效果。有趣的是,当我尝试使用旧的 Firefox 版本(46)时,我可以在没有任何第三方库的情况下截取完整的屏幕截图。我正在尝试使用最新的 firefox 并拥有完整的屏幕截图功能。

有什么帮助吗?

【问题讨论】:

标签: java selenium selenium-chromedriver geckodriver ashot


【解决方案1】:

试试:

Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(ShootingStrategies.scaling(1.75f), 1000)).takeScreenshot(driver);

其中 1.75f 是设备像素比(您可以在浏览器控制台中运行 window.devicePixelRatio; 来查找它)。 如果仍然无法捕获全屏,请将其更改为 2f

【讨论】:

  • 我需要在视口粘贴中添加缩放策略以使其正常工作,否则会被切断。感谢您为已接受的答案添加补充有用的信息。
【解决方案2】:

在使用 Selenium Java Client v3.12.0ChromeDriver v2.40Chrome v 67.0 时使用 ashot-1.4。 4.jar 这里是一个使用ChromeDriveraShot Library完整页面截图的例子。 em>网址 https://jquery.com/:

  • 代码块:

    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();
        }
    }
    
  • 截图:


参考

您可以在How to take screenshot with Selenium WebDriver找到详细讨论

【讨论】:

  • 尝试了一些旅游网站,如booking.com/expedia.com,结果是:纵向完整但横向一半。
  • ShootingStrategies 在 1.4.4 版本中不可用,但在 1.5.2 版本的 aShot 中可用。我只是在尝试。
  • @DebanjanB 如何以完全水平尺寸筛选完整的垂直尺寸?即 1920xFullSize、1280xFullSize 等...
  • @EugeneTruuts 默认策略应该有效。否则您能否根据您的确切要求提出一个新问题,以便我为您的问题构建一个答案?
猜你喜欢
  • 2019-01-24
  • 2021-07-12
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2017-10-20
  • 2017-06-02
相关资源
最近更新 更多