繁体   English   中英

Selenium:无法使用 aShot 库截取完整的页面截图

[英]Selenium: Not able to take complete page screenshot using aShot library

我正在尝试使用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 并具有完整的屏幕截图功能。

有什么帮助吗?

尝试:

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

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

在使用Selenium Java Client v3.12.0ChromeDriver v2.40Chrome v 67.0使用ashot-1.4.4.jar 时,这里是一个使用ChromeDriverurl https://jquery.com/ aShot 库水平和垂直截取完整页面截图的示例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 中找到详细的讨论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM