2016-09-22 67 views
2

我正在嘗試拍攝網頁的完整內容屏幕快照。但我只使用以下代碼獲得基於視圖的屏幕截圖。瀏覽器是Firefox我正在使用SELENIUM 3網絡驅動程序。使用硒3 webdriver捕獲網頁內容的全屏幕拍攝

File scrFile5 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
      try { 

       FileUtils.copyFile(scrFile5, new File("C:\\test.jpg")); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

我怎麼能達到同樣的效果。

+0

覺得我們只能採取截圖的網頁的可見部分,但我們沒有任何選擇採取截屏整網頁通過向下/向上滾動,甚至無法通過PrintScreen/SysRq –

+0

感謝您的回覆,但爲了防火狐狸硒硒,我設法採取整個內容的屏幕截圖。但不能通過Java代碼 –

回答

0

您可能需要使用RemoteWebDriver接口。你使用Firefox還是IE?見下面

File screenshotFile = null; 
      try { 
       // This checks to make sure we're not running an 
       // incompatible driver to take a screenshot (e.g. 
       // screenshots are not supported by the HtmlUnitDriver) 

       //the following line will throw a ClassCastException if the current driver is not a browser typed driver 
       RemoteWebDriver compatibleDriver = (RemoteWebDriver) driver; 

       screenshotFile = ((TakesScreenshot) compatibleDriver).getScreenshotAs(OutputType.FILE); 
      } catch (ClassCastException e) { 
       //this driver does not support screenshots. 
       // this should get logged as a warning -- this only means the driver cannot be of type RemoteWebDriver 

      } finally { 
       if (screenshotFile == null) { 
        System.out.println("This WebDriver does not support screenshots"); 
        return; //get us outa here 
       } 
      } 

      try { 

       String pathString = "some path of your choice" 
       File path = new File(pathString); 

       if (!path.exists()) 
        path.mkdirs(); 

       stringPath.concat("/someFileName.png"); 

       File newFileLocation = new File(pathString); 
       System.out.println("Full path to screenshot: " + newFileLocation.getAbsolutePath()); 

       FileUtils.copyFile(screenshotFile, newFileLocation); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
+0

我使用SELENIUM 3和瀏覽器是Firefox。你有任何特定於Firefox的代碼片段,這將是幫助。謝謝 –

+0

@Amit Sharma您是否嘗試使用我發佈的代碼? –

+0

我已經嘗試了上面的代碼,它採取的屏幕截圖視圖,沒有捕獲完整的內容,我能夠實現使用selenium IDE爲Firefox,所以我遇到的問題,當我試圖實現與webdriver相同。 –