Utilizing Selenium webdriver to retrieve images from the internet

Currently, I am attempting to retrieve images from a website using the following code snippet:

List <WebElement> listofItems = driver.findElements(By.cssSelector("div.heroThumbnails:nth-child(4) > div:nth-child(2) > div:nth-child(n)"));
URL imageURL = null;
for (WebElement myElement : listofItems) {
  String j = myElement.getAttribute("data-bigurl");
  System.out.println(j);
  for (int i = 0; i < listofItems.size(); i++) {
    try {
      // generate url
      imageURL = new URL(j);
      int countF = 0;
      // read url and retrieve image
      BufferedImage saveImage = ImageIO.read(imageURL);
      
      // save image in workspace with name picture.png
      ImageIO.write(saveImage, "jpg", new File(countF++ + ".jpg"));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

However, whenever the image is saved, it seems to overwrite the previous one. Is there a solution to prevent this issue? Any guidance on how to resolve this matter would be greatly appreciated. Thank you.

Answer №1

Perhaps you could find this useful:

ImageIO.write(saveImage, "jpg", new File(i + ".jpg"));

It seems like the issue might be that you are overwriting because you are giving them the same name.

To avoid this, you can either use a loop counter at the top or declare your countF variable outside of the loop:

int countF = 0;
for(int i = 0; i < listofItems.size(); i++) {
    try {
        // all your code here
        // ...

        // This will create an image with a new name each time
        ImageIO.write(saveImage, "jpg", new File(countF + ".jpg"));
        countF++;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I hope this solution works for you.

Happy Coding :)

Answer №2

Make sure to declare your countF variable outside of the loop so that its value is maintained across images.

    int countF = 0;
    for(int i = 0; i < listofItems.size(); i++){
        try {
            //generate url
            imageURL = new URL(j);
            //read url and retrieve image
            BufferedImage saveImage = ImageIO.read(imageURL);

            //download image to the workspace where the project is, save picture as picture.png (can be changed)
            ImageIO.write(saveImage, "jpg", new File(countF++ + ".jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

On a side note: the code line new File(countF++ + ".jpg")); may seem a little messy with multiple plus signs in a row for incrementing inline. An alternative approach could be:

countF++;
new File(countF + ".jpg"));

However, this is simply a matter of personal coding style preference and either method should function correctly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Behat Mink: dispatch a key stroke to the element in focus

I am looking to verify if the tab key navigation is functioning properly on my system. Here is the snippet of code I am using: define('TAB_CHARCODE', 9); define('NO_MODIFIER', ''); // .... $this->getSession()->getPage() ...

Clearing up confusion around identifying checkbox elements using XPath in Selenium WebDriver with Java, specifically within a

HTML code <div class="modal-footer slds-modal__footer" data-aura-rendered-by="2805:0"> <div class="rssDialogText grantAccessCheckbox packagingSetupUIRssDialogFooter" data-aura-rendered-by="2595:0" data-aura-class="packagingSetupUIRssDialogFooter" ...

Finding and saving several elements using selenium

In my current project, I am trying to locate and save the positions of certain elements so that a bot can click on them even if the page undergoes changes. While researching online, I found suggestions that storing the location of a single element in a var ...

Look for a specific element on the page and choose the "Next" option if the element

In my dynamic table, I have various links and I need to search for a specific link. this.table.element(by.cssContainingText('a','1616050')); If the link is not found on the current page, I want to click on the 'Next 30 records&ap ...

I am attempting to automate the login process on a website by accessing the password field using selenium. Despite utilizing the find_element_by_xpath method, I have been unsuccessful

Struggling to access password input fields on a website page using selenium Various methods such as find_element_by_xpath and by_id have been attempted import csv from selenium import webdriver from selenium.webdriver.support.select import Select import ...

Is there a way to detect when the user closes a tab or browser in HTML?

I am currently developing a web application using the MVC architecture and I need to find a way to detect when a user closes their browser tab so that I can destroy their session. My tech stack includes jsp (html, js) and java. Any suggestions on how to ...

Omitting certain values in jackson

I am currently working with Object Mapper and I am seeking a way to exclude certain fields based on specific values. Imagine having an object structured like this: public static class Data { int id; int value; } Let's assume that the value ...

Export Page Elements from Selenium to a File

I've created a script using Selenium IDE for Firefox on Windows 7 with FF 25.01 IDE version 2.4.0. The script is functioning well, but I'm interested in saving a particular page element from the query it executes to a text file. The page being l ...

Decode the implicit list in JSON format provided by the keys "1" and "2"

After retrieving this JSON data from an API, I need to convert it into a Java object. Here is the JSON structure: { "message" : "message", "1": { "packageCode": "packageCode1", " ...

Determining Visibility in Selenium: isDisplayed() or isVisible()?

Can you explain the distinction between isDisplayed() and isVisible() functions in Selenium? These two methods are commonly employed to determine whether a web element is hidden on a webpage or not. ...

Python Selenium - Exploring methods to locate and interact with a specific button

I've been struggling to select and click a particular button that will add a trading view strategy to a chart. Everything else in my automation process works smoothly, but this specific button is causing some trouble. Below is the code snippet I have ...

What could be the reason that the save password dialog does not appear when Selenium initiates Chrome?

After extensive research on the topic, I have yet to find a solution that works. I consider myself knowledgeable about Selenium and am puzzled as to why things work on Edge (Chromium-Based) but not on Chrome. Launching browser with Selenium I use the sa ...

Combine video using ffmpeg (or similar tools) while incorporating additional elements (such as overlays)

I am currently in the process of scraping clips from Twitch and combining them into a single video file. I have successfully managed to scrape Twitch clip links, but I am only able to get 16-20 videos due to the need to scroll with Selenium. While this is ...

Tips on changing textbox values in Page Object Model for both incrementing or decrementing

I need to enter a numeric value into this textbox, but I am having trouble using xpath because the id keeps changing. Below are the image and source code related to this issue. https://i.stack.imgur.com/yPKsg.jpg Below is the source code where I was able ...

Automatically generate an .au3 script using my C# code

I am faced with a challenge of automatically compiling an .Au3 script code and then running it. The script (au3 file) is updated automatically, but in order for the updates to take effect when I run the script, it needs to be compiled first. While there ar ...

Utilizing Selenium WebDriver for Automated Testing of Dropdown Menus

When it comes to selecting an option from a dropdown box, there are multiple approaches that can be taken. I have previously used the following method: driver.findElement(By.id("selection")).sendKeys("Germany"); However, this method did not always guaran ...

Selenium tips: Tricks to get past Cloudflare's bot protection

For educational purposes, I am looking to extract information from a website. However, I am facing obstacles due to the protection measures in place. Every time I try to send requests, I encounter the familiar "Checking-your-browser" page followed by con ...

Automating selection of dates with Selenium in C#

I am currently diving into Selenium and I am looking to hone my skills by automating the flight search process on a specific website, . Can anyone provide me with guidance on automating the start journey date picker in a way that allows for flexibility wit ...

Is it possible to interact with Java SE jars using JavaScript?

I need to integrate an API that is written in Java with a Client written in JavaScript. Is there any way to use this API, possibly along with other Java-SE JARs, in Webstorm? Any assistance would be greatly appreciated. Thank you! ...

Using the Selenium driver to locate items that are not present in the DOM after making an API call

In my work with complex Angular apps, I often encounter a situation where I need to select items in dropdowns that are not initially rendered in the DOM. These items are fetched from an API call and only appear in the DOM within span tags after they have b ...