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

huangapple go评论80阅读模式
英文:

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&lt;=sheet.getLastRowNum(); i++) {   
if(SearchKey.equals(result.getText()))
{        
System.out.println(&quot;Search is pass!&quot;);
Thread.sleep(3000);
WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.className(&quot;search-result__result-link&quot;)));
link.click();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
Thread.sleep(3000);
WebElement next = driver.findElement(By.partialLinkText(&quot;employees on LinkedIn&quot;));
next.click();
Thread.sleep(3000);
List&lt;WebElement&gt; list= driver.findElements(By.cssSelector(&quot;.search-results__list &gt; li&quot;));
System.out.println(&quot;Total number items :&quot;+list.size());
//Scroll and capture screenshot
for(j= 1; j&lt;=list.size(); j++)
{
WebElement elements= driver.findElement(By.cssSelector(&quot;.search-results__list &gt; li&quot;));    
//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 = &quot;E:\\Screenshots\\&quot;;			    
Thread.sleep(3000);
//Scroll when element gets hide
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(&quot;arguments[0].scrollIntoView();&quot;, elements);  
Thread.sleep(3000);         
FileUtils.copyFile(screenshots, new File (location + &quot;img&quot; + j+ &quot;.jpg&quot;));
}
int sum = sum + list.size();
System.out.println(&quot;Total number of items available&quot;+sum);
}	
else {
System.out.println(&quot;Search is fail&quot;);
}
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 + &quot;img&quot; + i + &quot;-&quot; + j + &quot;.jpg&quot;));
  }
}

huangapple
  • 本文由 发表于 2020年7月23日 19:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63052704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定