0

I have to print all the text of the web elements, so i am storing the web elements in list "test" and then getting text of each web element and keep them adding to other list "Title". Now when i am trying to print all the elements of list "Title".But only the text of 1st element is printed. Please help me to find where i am going wrong.

public void PrintText() 
{            
    var Title = new List<string>();
    IList <IWebElement> test=Controls.GetWebElementList(X-path);

    foreach (var g in test)
    {
        Title.Add(Controls.GetText(x-path));           
    }

    foreach (var h in Title)
    { 
        Console.WriteLine(h);
    }
}
Zalhera
  • 619
  • 5
  • 18

2 Answers2

0

It's not that clear how Controls.GetWebElementList() is defined.

Ideally to extract the texts you have to induce WebDriverWait for VisibilityOfAllElementsLocatedBy() and you can use the following Locator Strategy:

IList <IWebElement> test = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("X-path")));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Your code looks fine.

Try to verify the first list to print their values. enter image description here

And then run again, maybe your first line had only one value.

enter image description here

manolo386
  • 69
  • 1
  • Thank u so much... My problem is solved now :) I had just made this change Title.Add(g.Text); Thanks for the Help – Akanksha Jan 23 '22 at 07:53