Search code examples
c#seleniumwebdriver

Selenium c# Ilist Iweb Element not getting the text of all elements


im using IList to get a list of elements and the text value to be able to click on the correct element

IList<IWebElement> PONumbers = driver.FindElements(By.ClassName("first-column"));
        
        int POCount = PONumbers.Count;
        
        for (int i = 0; i < POCount; i++)
        { 
            String PONo = PONumbers.ElementAt(i).Text;
            
            if (PONo.Equals(PONoNonBatchNonTonne))
            {
                PONumbers.ElementAt(i).Click();
                break;
            }
        }

It gets all the correct elements which is 36

It only gets the text for the first 8 elements and then the rest are showing as ""

this is the html code:
this is the html code

it seems to be only getting the text of the elements in the view, but then why would it get all elements

any help welcome?


Solution

  • Since the element is hidden on webpage you have to use textContent attribute to get the value. To click on the element either scroll the page or use javaScript executor to click on the specific element.

    IList<IWebElement> PONumbers = driver.FindElements(By.ClassName("first-column"));
            
            int POCount = PONumbers.Count;
            
            for (int i = 0; i < POCount; i++)
            { 
                String PONo = PONumbers.ElementAt(i).GetAttribute("textContent");
                
                if (PONo.Equals(PONoNonBatchNonTonne))
                {
                    IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
                    executor.ExecuteScript("arguments[0].click();", PONumbers.ElementAt(i));                
                    break;
                }
            }