Download First 10 Images Using Selenium in C# on Google Images URL

Ashir Ali 21 Reputation points
2021-07-02T00:04:06.257+00:00

I want to download first 10 images from the given Url stated in the following code of google images.. I have tried soo much ways but can't be able to download images because i got the wrong lengthy urls which seems to be junk data.

Here is my code as i am using C#,

var driver = new ChromeDriver();

driver.Navigate().GoToUrl("https://www.google.com/search?q=wallpapers+pics&tbm=isch&ved=2ahUKEwizlL6W6sLxAhUE_4UKHS_YBDEQ2-cCegQIABAA&oq=wallpapers+pics&gs_lcp=CgNpbWcQAzICCAAyAggAMgIIADICCAAyAggAMgIIADICCAAyAggAMgIIADICCAA6BwgAELEDEEM6BAgAEENQ3I8FWJ6YBWDsnQVoAHAAeACAAasCiAHYCZIBAzItNZgBAKABAaoBC2d3cy13aXotaW1nwAEB&sclient=img&ei=bjXeYLOlJ4T-lwSvsJOIAw&bih=802&biw=1707");

// These are commented three ways to select the list of images

    //IList<IWebElement> Imghref = driver.FindElements(By.XPath("//img[@jsname]"));

    //IList<IWebElement> Imghref = driver.FindElements(By.ClassName("rg_i"));

    //IList<IWebElement> Imghref = driver.FindElements(By.TagName("img"));

    IList<IWebElement> Imghref = driver.FindElements(By.ClassName("rg_i"));

    //BcuVif - n3VNCb     --- ClassNames which i have observed

    foreach (IWebElement eachLink in Imghref)
    {
        eachLink.Click();

        IWebElement Images = driver.FindElement(By.TagName("img"));
        //Console.WriteLine(Images.GetAttribute("class"));
        String ImageUrl = Images.GetAttribute("src");
        string ImageName = Images.GetAttribute("alt");
        Console.WriteLine("Image URL : " + ImageUrl);
        WebClient downloader = new WebClient();
        downloader.DownloadFile(ImageUrl, "D:\\VisualStudio Workspace\\Download-Images\\images\\" + ImageName + ".jpg");

    }

Kindly any possible fix .. this will be very much helpful for me.

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-07-02T05:42:06.113+00:00

    Due to IT policy restrictions, I cannot use Selenium, try if this code can work for you.

           static void Main(string[] args)  
            {  
                List<string> ImageList = GetAllImages();  
                using (WebClient client = new WebClient())  
                {  
                    for (int i = 1; i < ImageList.Count; i++)  
                    {  
                        client.DownloadFile(new Uri(ImageList[i]), @"C:\Users\...\"+i+".jpg");  
                    }  
                }  
            }  
      
            public static List<string> GetAllImages()  
            {  
                List<string> ImageList = new List<string>();  
                WebClient x = new WebClient();  
      
                string source = x.DownloadString(@"https://www.google.com.hk/search?q=wallpapers+pics&tbm=isch&ved=2ahUKEwizlL6W6sLxAhUE_4UKHS_YBDEQ2-cCegQIABAA&oq=wallpapers+pics&gs_lcp=CgNpbWcQAzICCAAyAggAMgIIADICCAAyAggAMgIIADICCAAyAggAMgIIADICCAA6BwgAELEDEEM6BAgAEENQ3I8FWJ6YBWDsnQVoAHAAeACAAasCiAHYCZIBAzItNZgBAKABAaoBC2d3cy13aXotaW1nwAEB&sclient=img&ei=bjXeYLOlJ4T-lwSvsJOIAw&bih=802&biw=1707");  
      
                HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();  
      
                document.LoadHtml(source);  
      
                foreach (var link in document.DocumentNode.Descendants("img").Select(i => i.Attributes["src"]))  
                {  
                    ImageList.Add(link.Value);  
                }  
                return ImageList;  
            }  
        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.