0

I'm working on an application that downloads images from the internet in Selenium. However, I get the same error in the code seen in the picture and I cannot continue the process. This code, you can see the error in the image below.

        IWebDriver driver;
        int PictureID = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            var ChromeService = ChromeDriverService.CreateDefaultService();
            driver = new ChromeDriver(ChromeService);
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
            driver.Navigate().GoToUrl("https://oblivious212.artstation.com/");
            var Projects = driver.FindElements(By.ClassName("album-grid-item"));
            for(int i = 0; i < Projects.Count(); i++)
            {
                if (Projects.ElementAt(i) == null)
                {
                    continue;
                }
                Projects[i].Click();
                var Images = driver.FindElements(By.TagName("img"));
                for(int x = 0; x < Images.Count(); x++)
                {
                        PictureID++;
                        WebClient Downloader = new WebClient();
                        var ImageUrl = Images[x].GetAttribute("src");
                        var ImageName = Images[x].GetAttribute("alt");
                        Downloader.DownloadFile(ImageUrl, "C:\\Users\\DeLL\\Pictures\\Images\\" + ImageName + PictureID + ".jpg");
                }
            driver.Navigate().Back();
            }

Screenshot of exception when running in debug mode:

Error is here.

How do I solve this?

1 Answer 1

1

As soon as you navigate to a new page, which I guess is what your Projects[i].Click(); call does, any IWebElement objects you saved from an earlier page (oblivious212.artstation.com/) become "stale" and you can no longer use them. You must design your code around this fact; there are several ways you might do this.

Basically, while you're still on page oblivious212.artstation.com/, you need to save off any data you need from the IWebElement objects returned by your driver.FindElements(By.ClassName("album-grid-item")) call, into a local object, rather than saving the IWebElement objects themselves. Then, replace your Projects[i].Click(); call with code which uses your saved local data, rather than using the IWebElement objects themselves.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.