Home > front end >  Is that possible to save pictures with a name from page?
Is that possible to save pictures with a name from page?

Time:09-24

I want to save images whole products inside a site with their own names which is written on same page, is it possible to do that on a site with below logic?

Main product page has link for whole product at same page so I think I can manage to get every product here, in product page there is sub menus such as "General - Gallery etc." I want to get product name from General section then go to Gallery section and save images with this name like ProductName1.jpg, ProductName2.jpg ...

Is it possible or impossible to do with selenium?

Product Page: http://www.laboory.com/products

Here a sample link for product: http://www.laboory.com/product/laboory-water-soluble-m/3937

CodePudding user response:

Yes, we can do this. As you mentioned selenium tag only I assume it's using Java.

  • Go to the product page
  • Get image source URL and product name.
  • Using BufferedImage and ImageIO classes save the image into desired location.

Code:

driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();

driver.get("http://laboory.com/product/laboory-water-soluble-m/3937");
             
WebElement logo = driver.findElement(By.xpath("(//span//img[@class='imgin' and @src])[1]"));
String logoSRC = logo.getAttribute("src");
String productName = driver.findElement(By.xpath("//div/h1")).getText();
     
URL imageURL = new URL(logoSRC);
BufferedImage saveImage = ImageIO.read(imageURL);
ImageIO.write(saveImage, "png", new File(productName ".png"));

Output: The product CAPSULE GC 510.png saved in project directory.

Note: You can change the location as well.

CodePudding user response:

You can capture the screen shot of the image by using the dimension of the image element, and save it with desired name, below is the reference How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?

  • Related