0

I have a IReadOnlyCollection list with IWebElement. when i iterate through each element and try to display every elements ,it displays either blank text or "Element (id = #some random id)" as output. How do i display the elements as text?

Expected output should be like :

Round Trip

One Way

There is a similar question, but it didn't help :How can I access a element of a IReadOnlyCollection through it index?

Tried these options,

Console.WriteLine(list.ElementAt(1));
Console.WriteLine(list.ElementAt(1).Text);
Console.WriteLine(list.ElementAt(1).ToString());

Below is the code for reference:

 IReadOnlyCollection<IWebElement> list = Browser.Driver.FindElements(By.Name("tripType"));
 for (int i = 0; i < list.count; i++)
    {
      Console.WriteLine(list.ElementAt(i));
      Console.WriteLine(list.ElementAt(i).Text);
      Console.WriteLine(list.ElementAt(i).ToString());
     }

Here is the HTML:

 <font face="Arial, Helvetica, sans-serif" size="2"> 
 <input type="radio" name="tripType" value="roundtrip" checked="">
         Round Trip &nbsp; 
  <input type="radio" name="tripType" value="oneway">
          One Way</font>
Bharath
  • 113
  • 1
  • 1
  • 10

1 Answers1

1

I think you must use in the loop the GetAttribute, because there is no text in your HTML:

IReadOnlyCollection<IWebElement> list = Browser.Driver.FindElements(By.Name("tripType"));
 for (int i = 0; i < list.count; i++)
    {
      Console.WriteLine(list.ElementAt(i).GetAttribute("value");
     }

And One improvement I would use is a foreach loop

IReadOnlyCollection<IWebElement> list = Browser.Driver.FindElements(By.Name("tripType"));
    Foreach(IWebElement element in list){
        Console.WriteLine(element.GetAttribute("value");
    }
Gsanez
  • 61
  • 6
  • One Way why is it not possible to access the text "One Way" by using element.Text ? – Bharath Apr 16 '20 at 17:00
  • 1
    Because for you to get to use the text part must be like this in the HTML THIS IS TEXT The inputs usually work that way, the text you input can be retrieve by ".text" but the atributes that you are seeing as test are only retrive by using getAttribute() Hope it solves it for you – Gsanez Apr 17 '20 at 19:28