簡體   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