1

I'm trying to write an Program that will automatically make a google-pictures-search and download the first image of the given String.

I'm doing it all with selenium webdriver for Google, but I can change it tho. I tried to filter the results, but the only thinks that appears different to me is the "data-atf"-attribute. I want to download the first, so it should be on zero, but how Can I search after that? Besides the other attributes always change because of the different String that is given.

String = "German Shepherd"
ChromeDriver driver = new ChromeDriver();
driver.get("https:/google.com/search?q=" + String + 
"&source=lnms&tbm=isch&sa=X&ved=0ahUKEw
 iXlMO0nq_jAhUEzaQKHVVXC50Q_AUIEygE&biw 
 =834&bih=770");



//and then I've got something like this
//wont work because cssSelector is always different
WebElement img = driver.findElement(By.cssSelector("#selector"));
BufferedImage buffer = ImageIO.read(new URL(img.getAttribute("src")));
ImageIO.write(buffer, "png", new File("image.png"));
} catch (Exception e) {
    e.printStackTrace();
} finally {
    driver.close();
}

Credits for the second part to: Save/copy to clipboard image from page by chrome console

I need help most importantly to filter the result and after that helping to download would be highly appreciated.

1 Answers1

1

If you want to filter the images to the only those which have data-atf attribute the easiest is doing it via XPath selector

//img[@data-atf]

alternatively, if you want only children of "Search Results":

//h2[text()='Search Results']/parent::*/descendant::img[@data-atf]

Of course you can also filter images in Java code using Stream.filter() function

List<WebElement> allImages = driver.findElements(By.tagName("img"));
System.out.println("All images #: " + allImages.size());

List<WebElement> imagesWithDataAtf = allImages
        .stream()
        .filter(image -> image.getAttribute("data-atf") != null)
        .collect(Collectors.toList());

System.out.println("Images with data-atf attribute #: " + imagesWithDataAtf.size());
Dmitri T
  • 159,985
  • 5
  • 83
  • 133