英文:
screenshot is getting overwrite in next iteration of for loop or may be not taking screenshot in 2nd iteration
问题
以下是您提供的代码段的翻译部分:
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
if (SearchKey.equals(result.getText())) {
System.out.println("搜索通过!");
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("总项目数:" + list.size());
// 滚动并捕获截屏
for (int j = 1; j <= list.size(); j++) {
WebElement element = driver.findElement(By.cssSelector(".search-results__list > li"));
// 获取整个页面的截图
File screenshots = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshots);
// 获取元素在页面上的位置
Point point = element.getLocation();
// 获取元素的宽度和高度
int eleWidth = element.getSize().getWidth();
int eleHeight = element.getSize().getHeight();
// 裁剪整个页面截图,以获取仅元素截图
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
String location = "E:\\Screenshots\\";
Thread.sleep(3000);
// 元素隐藏时滚动
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", element);
Thread.sleep(3000);
FileUtils.copyFile(screenshots, new File(location + "img" + j + ".jpg"));
}
sum = sum + list.size();
System.out.println("可用项目总数:" + sum);
} else {
System.out.println("搜索失败");
}
searchlist.clear();
}
请注意,我已经对您提供的代码进行了简单的翻译,但可能会因为上下文等因素而导致某些细微差异。如果需要更多帮助或有任何疑问,请随时询问。
英文:
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();
}
答案1
得分: 0
你的外部循环声明了i
,你的内部循环也使用了i
,但它使用的是与外部循环相同的i
。这是一个非常棘手的逻辑,可能会导致许多意想不到的结果。。
所以,除非你确定你正在使用正确的逻辑,我建议你修复循环,以使用不同的变量,并使用这两个变量构建屏幕截图的名称,以便它们是唯一的。像这样:
for(int i=...){
for(int j=...){
FileUtils.copyFile(screenshots, new File(location + "img" + i + "-" + j + ".jpg"));
}
}
英文:
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"));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论