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.