How to download the all images from Website Using selenium webdriver tool

I was written below script that is not working in properly? Please help me?

package getAllImages;

import java.io.File;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Read_Images {
WebDriver driver;
@BeforeClass
public void GetBrowser()
{
driver = new FirefoxDriver();
driver.get(“http://www.flipkart.com”);
}
@AfterClass
public void End()
{
driver.close();
}
@Test
public void getImage() throws Exception
{
File files = null;
int loop= 0;

Thread.sleep(5000);
List <WebElement> Images = driver.findElements(By.tagName(“img”));
System.out.println(“No_Of Images:” +Images.size());

for(WebElement img :Images)
{
if(img.isDisplayed())
{

// Get the all images name and display;

loop++;
Thread.sleep(3000);
String[] name = img.getAttribute(“src”).split(“/”);
String ImgName = name[name.length-1];
System.out.println(ImgName);

// Save the all Images:

URL imag = new URL(img.getAttribute(“src”).trim());
files = new File(“D:\\TEMP\\”+ System.currentTimeMillis()+ “.” +ImgName);

org.apache.commons.io.FileUtils.copyURLToFile(imag, files);
}
}
System.out.println(“No_of Total downloaded images is :”+loop);
}
}

If I Run this code i got below error,

D:\TEMP\1461562718829.Damden-Vivo-Thumbnail_2.jpg?1455952362 (The filename, directory name, or volume label syntax is incorrect)

My doubt is,
what is the error, and why it is occur?

Default Asked on April 25, 2016 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)

    ImgName= 1461562718829.Damden-Vivo-Thumbnail_2.jpg?1455952362

    You image name should not contain ‘?’ mark. Please look where it is coming from and resolve it.

    Note: A filename cannot contain any of the following characters:

    \ / : * ? ” < > |
    Train Answered on May 3, 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.