英文:
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<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 hide
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", element);
Thread.sleep(3000);
FileUtils.copyFile(screenshots,new File(location + "img" + i + ".jpg"));
}
答案1
得分: 0
CopyFile 方法将 screenshots 复制到新的文件 location + "img" + i + ".jpg"。
英文:
Try something like
FileUtils.copyFile(screenshots,new File(location + "img" + i + ".jpg"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论