2016-10-02 108 views
2

所有喜來捕獲完整的網頁截圖,無法使用硒的webdriver 3.0.0-β3

我已經寫了下面的代碼以捕捉整個 網頁的截屏。

但是,我只能捕獲顯示在整個網頁上的網頁 的部分/可見部分。請建議。

IM使用:

硒webdriver的版本: 3.0.0-β3

的Firefox版本: 49.0.1

OS: Win10

package Selenium_WebDriver_Part1; 
    import java.io.File; 
    import java.io.IOException; 
    import java.text.SimpleDateFormat; 
    import java.util.Date; 
    import java.util.concurrent.TimeUnit; 

    import org.apache.commons.io.FileUtils; 
    import org.junit.Test; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.OutputType; 
    import org.openqa.selenium.TakesScreenshot; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.openqa.selenium.support.ui.ExpectedConditions; 
    import org.openqa.selenium.support.ui.WebDriverWait; 

    public class Quikr { 
     public static WebDriver driver=null; 
     @Test 
     public void loginTest() throws InterruptedException, IOException{ 
      System.setProperty("webdriver.gecko.driver","C:\\Eclipse\\Drivers\\geckodriver.exe"); 
      driver = new FirefoxDriver(); 
      driver.get("http://www.quikr.com/"); 
      driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); 
      takeScreenShot("LoginPage"); 
    } 
     public static void takeScreenShot(String fileName) throws IOException{ 
      Date now = new Date(); 
      SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh mm ss"); 
      String time = dateFormat.format(now); 
      String resDir = System.getProperty("user.dir")+"\\"+time; 
      File resFolder = new File(resDir); 
      resFolder.mkdir(); 
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
      // Now you can do whatever you need to do with it, for example copy somewhere 
      FileUtils.copyFile(scrFile, new File(resDir+"\\"+fileName+".jpeg")); 
     } 
    } 

回答

1

我最好的建議是使用第三方圖像處理庫阿紹特它幾乎解決了大多數的此類問題以及提供圖像比較等,一旦您導入阿紹特使用下面的代碼來獲得整個屏幕截圖一個網頁。

public byte[] takeScreenshot(){ 
    byte[] img = null; 
    Screenshot screenshot= new AShot() 
      .shootingStrategy(ShootingStrategies.viewportPasting(100)) 
      .takeScreenshot(driver); 
    BufferedImage image = screenshot.getImage(); 

    try { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     ImageIO.write(image, "jpg", baos); 
     baos.flush(); 
     byte[] imageInByte = baos.toByteArray(); 
     baos.close(); 
     img = imageInByte; 
     InputStream in = new ByteArrayInputStream(imageInByte); 
     BufferedImage bImageFromConvert = ImageIO.read(in); 
     URL location = CodeLocations.codeLocationFromClass(this.getClass()); 
     String path = location.toString().substring(location.toString().indexOf("file:/")+5,location.toString().length()); 
     ImageIO.write(bImageFromConvert, "jpg", new File(
       path+"/screenshot.jpg")); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return img; 
} 

嘗試阿紹特並分享您的想法

0

這是它應該如何工作。如果你想捕捉整個網頁,那麼你必須將整個內容縮小到可見區域,然後截取屏幕截圖。

0

爲了捕捉整個頁面的屏幕截圖,您需要在((TakesScreenshot)驅動程序).getScreenshotAs ...語句之前添加一行代碼。

driver = new Augmenter().augment(driver); 
2

我使用硒3.0.0 BETA4火狐OS v.43.0.1 Windows7的 代替System.setProperty( 「webdriver.gecko.driver」,「C :\ Eclipse的\ DRIVERS \ geckodriver.exe「);使用以下行 System.setProperty(「webdriver.firefox.marionette」,「D:\ Testing \ WebDriver \ geckodriver.exe」);