FirefoxDriver fd = new FirefoxDriver();
fd.get( “http://www.google.com”);
fd.findElement(By.name(“q”))。sendKeys(“bang bang”);
fd.findElement(By.name("btnG")).click();
fd.manage().window().maximize();
Thread.sleep(10000);
WebElement ww=fd.findElement(By.xpath(".//*"));
List<WebElement> lst=ww.findElements(By.tagName("img"));
System.out.println(lst.size());
for(int i = 1; i
{
File fg=fd.getScreenshotAs(OutputType.FILE);
BufferedImage bi=ImageIO.read(fg);
Point pq=ww.getLocation();
int h=ww.getSize().getHeight();
int w=ww.getSize().getWidth();
BufferedImage bg = bi.getSubimage(pq.getX(),pq.getY(),h,w);
ImageIO.write(bg, "png", fg);
FileUtils.copyFile(fg, new File ("f:\\bang.png"+i+".png"));
答案 0 :(得分:0)
也许我错过了这一点,但为什么不这样做:
List<WebElement> lwe = driver.findElements(By.cssSelector("img"));
上面会抓住所有IMG Dom元素
答案 1 :(得分:0)
您可以使用以下代码获取网页上的所有图片网址
WebDriver driver;
driver=new FirefoxDriver();
driver.get("http://yourpage.com");
List<WebElement> allImages = driver.findElements(By.tagName("img"));
for(WebElement imageFromList:allImages){
String ImageUrl=imageFromList.getAttribute("src");
System.out.println(ImageUrl); //will get you all the image urls on the page
}
您可以使用ImageIO读取图像。阅读BufferedImage,然后使用ImageIO.write将图像写入磁盘