question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] how to filter array of selectors with same ID by text

See original GitHub issue

I have the case where elements have the same data-test-id. Instead of using hardcoded indexes, I would like to get and then use the needed element by text.

I’m using the following code but filtering doesn’t work, get back the same array of elements with ElementHandlers

/// Util class
  async getElementsById(
    id,
    option: ElementContext = {
      locator: id,
      context: this.page,
    }
  ):Promise<Array<ElementHandle>>{
    return await option.context.$$(`[data-test-id=${option.locator}]`)
  }

/// Page object
 megaNavItem = async () =>
        await this.getElementsById('mega-nav-level-1-item')
        
  filterElements = async (expectedItem: string) =>
    await this.megaNavItem().then(el =>
      el.find(async e => (await e.innerText()) === expectedItem)
    )

Unfortunately, can’t understand what is the reason. I see that text is not extracting (innerText() doesn’t work ) to make correct filtering. Would appreciate any help with this case.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
aslushnikovcommented, Nov 11, 2021

@Yurii397 it looks like you got tripped with promises. Your filter function uses async predicate, which won’t work. You’ll need to use a sync predicate, and use Promise.all() construct beforehand or a manual iteration to fetch contents.

Something like this would work:

/// Util class
async function getElementsById(id, option) {
  return await option.context.$$(`[data-test-id=${option.locator}]`)
}

/// Page object
megaNavItem = async () => await this.getElementsById('mega-nav-level-1-item')

filterElements = async (expectedItem: string) => {
  const elements = await this.megaNavItem();
  const result = [];
  for (const element of elements) {
    const text = await element.innerText();
    if (text === expectedItem)
      result.push(element);
  }
  return result;
}
0reactions
Yurii397commented, Nov 11, 2021

@aslushnikov thank you for the guide!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Find all elements on a page whose element ID contains a ...
Hello, how can I use a selector to select those elements whose id belongs to an array. – bpa.mdl. May 17, 2018 at...
Read more >
DataWeave filter function: How to filter items in an Array
To use filter , you will need to pass two arguments: an Array of any type and a function (or a condition) to...
Read more >
ID selectors - CSS: Cascading Style Sheets - MDN Web Docs
The CSS ID selector matches an element based on the value of the element's id attribute. In order for the element to be...
Read more >
jQuery API Documentation
Attribute Not Equal Selector [name!=”value”] ... Get the children of each element in the set of matched elements, including text and comment nodes....
Read more >
get | Cypress Documentation
Get one or more DOM elements by selector or alias. The querying behavior of this command is similar to how $(...) works in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found