2016-09-21 68 views
1

我試圖抓取網頁上存在的所有元素的屏幕截圖,並且希望將其存儲在我已寫入以下代碼的磁盤中。抓取頁面上所有元素的屏幕截圖

唯一的問題是這段代碼只適用於第一次迭代,之後會發生一些意想不到的事情。

List<WebElement> eleId = driver.findElements(By.xpath("//*[@id]")); //fetch all the elements with ID attribute 
    System.out.println(eleId.size()); 

    for (int i = 0;i < eleId.size();i++) { 

//   Get entire page screenshot 
      File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); 
      BufferedImage fullImg = ImageIO.read(screenshot); 

//   Get the location of element on the page 
      Point point = eleId.get(i).getLocation(); 

//   Get width and height of the element 
      int eleWidth = eleId.get(i).getSize().getWidth(); 
      int eleHeight = eleId.get(i).getSize().getHeight(); 

//   Crop the entire page screenshot to get only element screenshot 
      BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); 
      ImageIO.write(eleScreenshot, "png", screenshot); 

//   Creating variables name for image to be stores in the disk 
      String fileName = eleId.get(i).getAttribute("id"); 
      String imageLocation = "D:\\" + fileName + ".png"; 
//   System.out.println(imageLocation); 

//   Copy the element screenshot to disk 
      File screenshotLocation = new File(imageLocation); 
      FileUtils.copyFile(screenshot, screenshotLocation); 

      System.out.println("Screenshot has been stored."); 
} 
+0

你是什麼意思意想不到的事情?你的腳本會拋出任何錯誤? –

+0

它不會拋出任何錯誤,但只運行一次迭代。 –

回答

1

嗨Sandeep嘗試下面的代碼。它爲我工作。我只是添加了一個「if」條件來檢查圖像的高度和寬度。

@Test(enabled=true) 
public void getIndividualElementScreenShot() throws IOException{ 
    WebDriver driver = new FirefoxDriver(); 
    driver.get("http://www.google.com/"); 
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); 
    driver.manage().window().maximize(); 
    List<WebElement> eles = driver.findElements(By.xpath("//*[@id]")); 
    System.out.println(eles.size()); 
    for(WebElement ele : eles){ 
     //Get Entire page screen shot 
     File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
     BufferedImage fullImage = ImageIO.read(screenShot); 

     //Get the location on the page 
     Point point = ele.getLocation(); 

     //Get width and height of an element 
     int eleWidth = ele.getSize().getWidth(); 
     int eleHeight = ele.getSize().getHeight(); 

     //Cropping the entire page screen shot to have only element screen shot 
     if(eleWidth != 0 && eleHeight != 0){ 
      BufferedImage eleScreenShot = fullImage.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); 
      ImageIO.write(eleScreenShot, "png", screenShot); 


      //Creating variable name for image to be store in disk 
      String fileName = ele.getAttribute("id"); 
      String imageLocation = "F:\\ElementImage\\"+fileName+".png"; 
      System.out.println(imageLocation); 

      //Copy the element screenshot to disk 
      File screenShotLocation = new File(imageLocation); 
      org.apache.commons.io.FileUtils.copyFile(screenShot, screenShotLocation); 

      System.out.println("Screen shot has beed stored"); 

     } 
    } 
    driver.close(); 
    driver.quit(); 
} 
+0

Ty @Sandipan Pramanik,我認爲問題在於我試圖訪問的元素的寬度和高度... tysm :) –

0

您的代碼運行良好。但是你在每次迭代中覆蓋文件。你明白了嗎?通過在fileName變量中分配新文件名來更改每次迭代中的文件名。使用下面的代碼:

 List<WebElement> eleId = driver.findElements(By.xpath("//*[@id]")); //fetch all the elements with ID attribute 
     System.out.println(eleId.size()); 

     for (int i = 0;i < eleId.size();i++) { 

//   Get entire page screenshot 
       File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
       BufferedImage fullImg = ImageIO.read(screenshot); 

//   Get the location of element on the page 
       Point point = eleId.get(i).getLocation(); 

//   Get width and height of the element 
       int eleWidth = eleId.get(i).getSize().getWidth(); 
       int eleHeight = eleId.get(i).getSize().getHeight(); 

//   Crop the entire page screenshot to get only element screenshot 
       BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); 
       ImageIO.write(eleScreenshot, "png", screenshot); 

//   Creating variables name for image to be stores in the disk 
       String fileName = eleId.get(i).getAttribute("id"); 
       String imageLocation = "D:/" + fileName + i + ".png"; 
//   System.out.println(imageLocation); 

//   Copy the element screenshot to disk 
       File screenshotLocation = new File(imageLocation); 
       FileUtils.copyFile(screenshot, screenshotLocation); 

       System.out.println("Screenshot has been stored."); 
    } 
+0

我也試過這個,但仍然是相同的輸出,它在第一次迭代後停止。 –

+0

複製並粘貼編輯答案中的代碼部分。我認爲,這會有所幫助。 :) –

+0

嘗試複製粘貼以及..但只有一個迭代。 –