0

I have list size of 10 items and a for loop which has 2 iterations so in the first iteration selenium captures the screenshot of all 10 items and but in 2nd iteration i don't either it's overwriting the all 10 screenshots captured in the first iteration or it is not capturing any screenshot for the next 10 items remaining, It has 10 items in 1st iteration and then in the 2nd it has total 20 so there should be 20 screenshots but i'm getting only 10, below is the code snippet, if anybody can help or suggest something what i'm doing wrong -:

for(int i=1; i<=sheet.getLastRowNum(); i++) {   

if(SearchKey.equals(result.getText()))
     {        
        System.out.println("Search is pass!");
        Thread.sleep(3000);
        WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.className("search-result__result-link")));
        link.click();
        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        Thread.sleep(3000);
        WebElement next = driver.findElement(By.partialLinkText("employees on LinkedIn"));
        next.click();
        Thread.sleep(3000);
        List<WebElement> list= driver.findElements(By.cssSelector(".search-results__list > li"));
        System.out.println("Total number items :"+list.size());
        
        //Scroll and capture screenshot
        for(j= 1; j<=list.size(); j++)
        {
            WebElement elements= driver.findElement(By.cssSelector(".search-results__list > li"));    
            
            //Get entire page screenshot
            File screenshots = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            BufferedImage  fullImg = ImageIO.read(screenshots);
            
            // Get the location of element on the page
            Point point = Decision_Maker.getLocation();
            
            //Get width and height of the element
            int eleWidth =  Decision_Maker.getSize().getWidth();
            int eleHeight = Decision_Maker.getSize().getHeight();
            
            //Crop the entire page screenshot to get only element screenshot
            BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
                eleWidth, eleHeight);
            String location = "E:\\Screenshots\\";              
            Thread.sleep(3000);
            
            //Scroll when element gets hide
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].scrollIntoView();", elements);  
            Thread.sleep(3000);         
            
            FileUtils.copyFile(screenshots, new File (location + "img" + j+ ".jpg"));
        }
        int sum = sum + list.size();
        System.out.println("Total number of items available"+sum);
    }   
    
    else {
      System.out.println("Search is fail");
    }
    searchlist.clear();
}
  • Where in your code there is a loop for 2 iteration? I can see only one loop iterating over the list. – Alexey R. Jul 23 '20 at 11:34
  • please check i have edited my code – Kajol Gupta Jul 23 '20 at 11:57
  • @KajolGupta If I am correct your current logic will end-up taking the same screenshot in the second for loop. And make sure to provide different iterator names in both for loops as shown in the below answer. – supputuri Jul 23 '20 at 12:37

1 Answers1

0

Your outer loop declares i and your inner loop also uses i but it uses the same i that is used by outer loop. This is a really tricky logic that might lead to a lot of unexpected results..

So unless you are sure you are using the right logic I suggest you to fix the loops in order to use different variables and build the screenshot name using both those variables so that they would be unique. Like:

for(int i=...){
  for(int j=...){
     FileUtils.copyFile(screenshots, new File (location + "img" + i + "-" + j + ".jpg"));
  }
}
Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • @ Alexey R. This is not because of var i & j yes i have mentioned it in the code but that was the typing mistake but i have corrected this and still the same issue arises – Kajol Gupta Jul 23 '20 at 12:46
  • Did you see the example I gave you? Pay attention to how the name for screenshot is built.. – Alexey R. Jul 23 '20 at 12:56
  • 1
    Alexey R.I'm sorry for not paying attention, but your code has worked out for me. Thanks! – Kajol Gupta Jul 23 '20 at 13:10