屏幕截图在 for 循环的下一次迭代中被覆盖,或者可能在第二次迭代中未截取屏幕截图

screenshot is getting overwrite in next iteration of for loop or may be not taking screenshot in 2nd iteration

我有 10 个项目的列表大小和一个有 2 次迭代的 for 循环,所以在第一次迭代中,selenium 捕获了所有 10 个项目的屏幕截图,但在第二次迭代中,我也没有覆盖捕获的所有 10 个屏幕截图在第一次迭代中或者它没有为接下来的 10 个剩余项目捕获任何屏幕截图,它在第一次迭代中有 10 个项目然后在第二次迭代中它总共有 20 个所以应该有 20 个屏幕截图但我只得到 10 个,下面是代码片段,如果有人可以帮助或建议我做错了什么 -:

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();
}

您的外循环声明了 i,而您的内循环也使用了 i,但它使用与外循环相同的 i。这是一个非常棘手的逻辑,可能会导致很多意想不到的结果..

因此,除非您确定您使用的是正确的逻辑,否则我建议您修复循环以使用不同的变量并使用这两个变量构建屏幕截图名称,以便它们是唯一的。喜欢:

for(int i=...){
  for(int j=...){
     FileUtils.copyFile(screenshots, new File (location + "img" + i + "-" + j + ".jpg"));
  }
}