Search code examples
listelementplaywrightplaywright-sharp

How can I iterate over a list of elements using Playwright in C# and return information from child elements too


I am trying to iterate over a list of elements and extract information from that element and its child elements.

<div id="parent-content-div">
   <a title="BBC" href="www.bbc.co.uk">
      <div class="category">News</div>
   </a>
   <a title="Google" href="www.google.com">
      <div class="category">Search</div>
   </a>
   <a title="YouTube" href="www.youtube.com">
      <div class="category">Entertainment</div>
   </a>
</div>

I have created a locator to identify the <a> elements within the parent div as follows:

ILocator Websites = _page.Locator("div[id='parent-content-div']").Locator("a");

This is what I've got so far (and allows me to extract the title and href).

foreach(var element in Websites.ElementHandlesAsync())
{
    Console.WriteLine(element.GetAttributeAsync("title"));
    Console.WriteLine(element.GetAttributeAsync("href"));
}

But how do I then query for further sub elements? Something like?

foreach(var element in Websites.ElementHandlesAsync())
{
    ...
    
    Console.WriteLine(element.GetInnerElement("div[class='category']")).Text;
}

Except that last line is pseudo code rather than something real!


Solution

  • OK, I got there..

    foreach(var element in Websites.ElementHandlesAsync())
    {
        Console.WriteLine(await element.EvalOnSelectorAsync<string>("div[class='category']), "sel => sel.innerText);
    }