2016-09-22 58 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相同。 –