none
Table with pager can not click button in cell after refresh (Selenium, RemoteWebDriver) RRS feed

  • Question

  • Hi,

    I have an HTML table with images in end of row in Action column like Empty, Export, Uplod.

    Table contains an Pager. I conver HTML data to List<RowDataCollection>. See below:

    private static List<RowDataCollection> rowDataCollections = new List<RowDataCollection>();
    
    class CellWithString
        {
            public int colNumber { get; set; }
            public int rowNumber { get; set; }
            public string colName { get; set; }
            public string value { get; set; }
        }
    
    class RowDataCollection
        {
            public int rowNumber { get; set; }
            public int pageNumber { get; set; }
            public List<CellWithString> listStrings { get; set; }
            public List<IWebElement> listButtons { get; set; }
            public List<IWebElement> listInputs { get; set; }
    
            public RowDataCollection()
            {
                listStrings = new List<CellWithString>();
                // listStrings = new Dictionary<string, string>();
                listButtons = new List<IWebElement>();
                listInputs = new List<IWebElement>();
            }
        }
    
    public static void ReadTableForRich(string tableXPath)
            {
                rowDataCollections.Clear();
                string headerXPath = tableXPath + ExcelUtil.ConfigValueByName(cfgTable, "tblHeaders"); // "/descendant::tr[contains(@id,'ch')]/th";
                string rowXPath = tableXPath + ExcelUtil.ConfigValueByName(cfgTable, "tblRows"); // "/descendant::tbody[contains(@id,'tb')]/tr";
                string columnXPath = "(" + rowXPath + ")" + ExcelUtil.ConfigValueByName(cfgTable, "tblColumns"); // [#index#]/td";
                var columns = driver.FindElements(By.XPath(headerXPath));
                var rows = driver.FindElements(By.XPath(rowXPath));
                int pageCount = GetPageCount();
                int currentPage = 1;
                int rowIndex = 1;
    
                while (currentPage <= pageCount)
                {
                    foreach (var row in rows)
                    {
                        int colIndex = 1;
                        RowDataCollection rowData = new RowDataCollection();
                        rowData.rowNumber = rowIndex;
                        rowData.pageNumber = currentPage;
                        var cols = driver.FindElements(By.XPath(columnXPath.Replace("#index#", rowIndex.ToString())));
                        foreach (var col in cols)
                        {
                            if (col.Text != "")
                            {
                                CellWithString cellWithString = new CellWithString();
                                cellWithString.colNumber = colIndex;
                                cellWithString.rowNumber = rowIndex;
                                cellWithString.colName = columns[colIndex - 1].Text;
                                cellWithString.value = col.Text;
                                rowData.listStrings.Add(cellWithString);
                            }
                            else if (col.FindElements(By.TagName("img")).Count() != 0)
                            {
                                foreach (IWebElement i in col.FindElements(By.TagName("img")))
                                {
                                    rowData.listButtons.Add(i);
                                }
                            }
                            else if (col.FindElements(By.TagName("input")).Count() != 0)
                            {
                                foreach (IWebElement i in col.FindElements(By.TagName("input")))
                                {
                                    rowData.listInputs.Add(i);
                                }
                            }
                            colIndex++;
                        }
                        rowDataCollections.Add(rowData);
                        rowIndex++;
                    }
    
                    if (currentPage < pageCount)
                    {
                        ClickOnPager(currentPage + 1);
                        Thread.Sleep(defaultDelay);
                        columns = driver.FindElements(By.XPath(headerXPath));
                        rows = driver.FindElements(By.XPath(rowXPath));
                        rowIndex = 1;
                        
                    }
                    currentPage++;
                }
    
                if (pageCount > 1) ClickOnPager(1);
                Thread.Sleep(defaultDelay);
            }

    Finally I wrote a function to click Action Image in selected row. 

    tableXPathe - xPath of table in DOM, value - key to find a row (for example: KiT_0), id - picture id in row (Empty-1, Export-2, Upload-3)

    public static void ClickImageBtnByID(string value, int id)
            {
                foreach (RowDataCollection r in rowDataCollections)
                {
                    foreach (CellWithString c in r.listStrings)
                    {
                        if (c.value == value)
                        {
                            if (r.listButtons.Count() > id)
                            {
                                ClickOnPager(r.pageNumber);
                                Thread.Sleep(defaultDelay);
                                r.listButtons[id].Click();
                                return;
                            }
                        }
                    }
                }          
            }

    My problem is the next:

    1. ReadTableForRich(tableXPath) i working OK. rowDataCollections filled with correct data.

    2. But when I call ClickImageBtnByID("KiT_0", 2) it doesn't work because page refreshed during ReadTableForRich changed.

    "OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document
      (Session info: chrome=72.0.3626.121)
      (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17134 x86_64)'"

    How can I fix this problem?

    THX

    KiT




    Tuesday, March 19, 2019 12:58 PM

All replies

  • Hi friend,

    This form is to talk about problems in visual studio unit testing. We recommend you to go to the Selenium Community or stack overflow with selenium-webdriver tag to get better support. Thank you for understanding.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com


    Wednesday, March 20, 2019 9:42 AM
  • Hi,

    I useing Selinium and C#.

    IWebElement i in col.FindElements(By.TagName("img"))

     columns = driver.FindElements(By.XPath(headerXPath));
     rows = driver.FindElements(By.XPath(rowXPath));

    BR 

    KiT

    Thursday, March 21, 2019 10:42 AM
  • Hi,

    Thank you for your reply.

    The selenium is third party software. For your issue which is more related to selenium, we suggest that you could go to the selenium community or stack overflow. More community members on that forum may further look at your issue and provide more suggestions.

    If you have issues about VS IDE , please feel free to contact us. We will try our best to give you a solution.

    Thank you for your understanding and cooperation.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com


    Friday, March 22, 2019 7:37 AM