如何使用for循环依次捕获所有'li'个元素的屏幕截图
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 hide
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", element);
Thread.sleep(3000);
FileUtils.copyFile(screenshots,new File(location + "img" + i + ".jpg"));
}
试试
FileUtils.copyFile(screenshots,new File(location + "img" + i + ".jpg"));
我在列表标签中有 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 hide
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", element);
Thread.sleep(3000);
FileUtils.copyFile(screenshots,new File(location + "img" + i + ".jpg"));
}
试试
FileUtils.copyFile(screenshots,new File(location + "img" + i + ".jpg"));