question

AshirAli-1085 avatar image
0 Votes"
AshirAli-1085 asked AshirAli-1085 commented

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

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.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered AshirAli-1085 commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AshirAli-1085
May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·

Can you do this to run multiple 10 instances of chrome to downloading 10-10 images sequentially using Nunit.

0 Votes 0 ·