0

I was writing a file download program from the internet with Selenium. But a few seconds after the program is started, I encounter this error.

        Thread.Sleep(500);
        var driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://oblivious212.artstation.com/");
        Thread.Sleep(1000);
        var Projects = driver.FindElements(By.ClassName("album-grid-item"));
        for(int i = 0; i < Projects.Count(); i++)
        {
            Projects = driver.FindElements(By.ClassName("album-grid-item"));
            Projects[i].Click();
            Thread.Sleep(1000);
            var Images = driver.FindElements(By.TagName("img"));

            for(int x = 0; x < Images.Count(); x++)
            {
                var ImageUrl = Images[x].GetAttribute("src");
                var ImageName = Images[x].GetAttribute("alt");
                WebClient Downloader = new WebClient();
                Downloader.DownloadFile(ImageUrl, "C:\\Users\\DeLL\\Pictures\\Images\\" + ImageName + ".jpg");
            }
            Thread.Sleep(250);
            driver.Navigate().Back();

Fault point.

  • 1
    Why is `Projects = driver.FindElements(By.ClassName("album-grid-item"));` inside the loop when it is already calculated outside the loop and is the basis for the loop? – Retired Ninja Apr 08 '22 at 21:03

1 Answers1

0

The variable Projects is already set outside the for loop. You do not have to get it again and again inside the loop. Also, you are iterating on its count and modifying the object you iterate on would give an exception inside a 'foreach' loop, in a for loop it also bad practice.

You can anyways check if the index is valid anyways in a condition.

if (Projects.ElementAt(i) == null) {
 continue; //index i is wrong - continue will propagate to next element in your for loop
}

Note that the ElementAt method needs to have the namespace System.Linq added to your code.

Tore Aurstad
  • 3,189
  • 1
  • 27
  • 22
  • Thank you, but unfortunately I don't know how to place the Foreach and do what you said. Can you help a little more? – pronoromoro Apr 08 '22 at 22:36
  • Try first removing the : Projects = driver.FindElements(By.ClassName("album-grid-item")); inside the for loop This variable is already set outside the for loop, no need to repeat it. Then add my code at the top of code inside your for loop. – Tore Aurstad Apr 08 '22 at 22:46
  • for(int i = 0; i < Projects.Count(); i++) { if (Projects.ElementAt(i) == null) { continue; } Projects[i].Click(); Unfortunately, I ran into a different error when I typed it this way. Error: OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document (Session info: chrome=100.0.4896.75)' – pronoromoro Apr 08 '22 at 23:37
  • check this answer also ? https://stackoverflow.com/questions/18225997/stale-element-reference-element-is-not-attached-to-the-page-document I think you now have more an issue with Selenium than a crash in c# – Tore Aurstad Apr 08 '22 at 23:40