1

I'm trying to get some data from a JSP page, the page has a table with pagination and i'm trying to get the values from each table page.

As i could see each element is created dynamically with a class and name set by the server with timestamp and other stuff so i have to use more generic data as possible on DOMs to get them.

Once the first page is clicked the page generate a second paginator which will be the real one..

The issue is that after i processed 3 pages from the paginator i get the following error:

'stale element reference: element is not attached to the page document (Session info: chrome=89.0.4389.114)'

But actually the DOM element exists on the page but it's recreated with other name and class but it should be get anyway...

Here is my code:

    var pages = searchResult.FindElements(By.ClassName("custom-pagination"));

    foreach (var page in pages[0].FindElements(By.TagName("input")))
    {
        
        var PageValue = page.GetAttribute("value");
        if (PageValue == "<<") continue;
        if (PageValue == ">") break;
        if (PageValue != "1")
        {
            page.Click();
            WaitUntilElementNotVisible(SelectorByAttributeValue("iawaitpanel", "true"), 30000, driver);
        }

        foreach (var azienda in driver.FindElements(By.ClassName("item_list")))
        {
            DataRow row = table.NewRow();
            row["REGIONE"] = NomiRegioni[IndexRegione];
            row["NOME AZIENDA"] = azienda.FindElement(By.ClassName("txtGreen")).Text;
            var indirizzo = azienda.FindElement(By.ClassName("sede")).Text.Replace("Sede Legale", "").Trim();
            row["INDIRIZZO"] = indirizzo;
            row["CAP"] = indirizzo;
            row["CITTA"] = indirizzo;
            row["MAPS"] = azienda.FindElement(SelectorByAttributeValue("title", "Vai a Google Maps")).GetAttribute("href");
            table.Rows.Add(row);
        }


    }

And here is a small fiddle of how the page looks like..

I've yed read this question and i've yet tried to set like in CATCH the code again where i get the element but it has no effect...

NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100

1 Answers1

1

Instead of calling FindElements each time in your foreach loop, call it and store your input elements in a variable. Then loop through those inputs, it appears to call FindElements each time on the same page?

  • i tried to store them in a variable but as on each element click the page generate the buttons again they are the same but different so i will be unable to find that inputs.. – NiceToMytyuk Apr 14 '21 at 06:39
  • What happens to the elements identifiers when you manually step through the pages? You mentioned above the classes and names change, I would log out what is happening with the inputs as you loop through and make the call to click, could be something that happens to that page after a certain amount of clicks. Could be reloading data or some other unforeseen event occurring. – John Rodriguez Apr 14 '21 at 14:55
  • i solved it by getting the elements via xpath in a do while by changing the one static value for all – NiceToMytyuk Apr 14 '21 at 14:57