dollar
dollar

Reputation: 3

how to capture all the images on a web page using WebDriver?

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"));

Upvotes: 0

Views: 11600

Answers (2)

srikanth
srikanth

Reputation: 9

You can use the below code to get all image urls on the webpage-

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
}

You can Read the image using ImageIO.Read to BufferedImage and then write the images to your disk using ImageIO.write

Upvotes: 0

bcar
bcar

Reputation: 815

Maybe I'm missing the point but why not do:

List<WebElement> lwe = driver.findElements(By.cssSelector("img"));

The above would grab all IMG Dom elements

Upvotes: 1

Related Questions