[Question] how to filter array of selectors with same ID by text
See original GitHub issueI 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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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 usePromise.all()
construct beforehand or a manual iteration to fetch contents.Something like this would work:
@aslushnikov thank you for the guide!