如何使用for循环逐个捕获所有’li’元素的屏幕截图。

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

How to capture screenshot of all 'li' elements one after another with for loop

问题

我有一个包含10个列表项的列表标签,想要逐一对列表项进行截屏,但我编写的代码却捕获整个页面的截屏,当剩余的 "li" 元素被隐藏时,希望能够滚动页面,然后再进行截屏。以下是代码片段:

Image Link -: https://i.stack.imgur.com/WB6Nh.png

List<WebElement> list = driver.findElements(By.cssSelector(".search-results__list > li"));
System.out.println("Total number of items :" + list.size());
//It returns with 10 items

//Scroll and capture screenshot
for(int i = 1; i <= list.size(); i++) {
    WebElement element = 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 hidden
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView();", element);   
    Thread.sleep(3000);         
    FileUtils.copyFile(screenshots, new File(location + "img" + i + ".jpg"));
}

Image Link -: https://i.stack.imgur.com/WB6Nh.png

英文:

I have a 10 items in list tag and want to take screenshot of one item of list at a time and so on but the code i have wrote capturing whole page screenshot and also want to scroll when remaining "li" elements is hidden so then only it should scroll and take screenshot. Below is the code snipped

        Image Link -: https://i.stack.imgur.com/WB6Nh.png
         
        List&lt;WebElement&gt; list = driver.findElements(By.cssSelector(&quot;.search-results__list &gt; li&quot;));
	    System.out.println(&quot;Total number of items :&quot;+list.size());
        //It returns with 10 items
	    
	    //Scroll and capture screenshot
	    for(int i= 1; i&lt;=list.size(); i++)
	    {
		    WebElement element = 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;, element);   
		    Thread.sleep(3000);         
		    FileUtils.copyFile(screenshots,new File(location +  &quot;img&quot; + i + &quot;.jpg&quot;));
	    }

答案1

得分: 0

CopyFile 方法将 screenshots 复制到新的文件 location + "img" + i + ".jpg"。

英文:

Try something like

 FileUtils.copyFile(screenshots,new File(location +  &quot;img&quot; + i + &quot;.jpg&quot;));

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

发表评论

匿名网友

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

确定