2016-09-21 2 views
1

J'essaie de capturer la capture d'écran de tous les éléments présents sur une page Web et que vous voulez stocker dans mon disque pour lequel j'ai écrit le code ci-dessous.Capture d'écran de tous les éléments présents sur une page

Le seul problème est que ce morceau de code ne fonctionne que pour la première itération et après quoi quelque chose d'inattendu se produit.

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

Que voulez-vous dire par des choses inattendues? est-ce que votre script lance une erreur? –

+0

Il ne génère aucune erreur mais s'exécute uniquement pour une itération. –

Répondre

1

Salut Sandeep essayez le code ci-dessous. Ça marche pour moi. Je viens d'ajouter une condition "if" pour vérifier la hauteur et la largeur de l'image.

@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, je pense que le problème était avec la largeur et la hauteur de l'élément que j'essayais d'accéder ... tysm :) –

0

Votre code se porte bien. Mais vous êtes en train d'écraser le fichier dans chaque itération. As-tu compris? Changez le nom du fichier dans chaque itération en affectant un nouveau nom de fichier dans la variable fileName. Utilisez le code ci-dessous:

 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

J'ai essayé cela aussi, mais toujours la même sortie, il s'arrête après la première itération. –

+0

Copiez et collez la partie de code de la réponse éditée. Je pense que ça aidera. :) –

+0

Pâte de copie essayé ainsi ... mais une seule itération. –