我有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"));
}
}