Given the following html:
...
<div class="table">
<div role="row" row-id="1">...</div>
<div role="row" row-id="2">...</div>
<div>
...
How can I use the getByRole('row')
locator to get the div with row-id="1"
?
Using getByRole('row').filter('[row-id="1"]')
doesn’t work as filter
seems to be applied on the child elements only.
However according to the documentation, the filter locator "is queried starting with the outer locator match", so the filter should actually work? Am I missing something?
Using the argument options.name
of getByRole()
is not an option.
2
Answers
You can use the and locator.
But I think you can solve that with just one CSS expression.
If you’re trying to assert on the text content, the cleanest approach is to use
getByRole
‘sname:
key:If you don’t want to assert on the text,
:scope
is also possible, allowing you to include the current node in the query context:I generally avoid
.filter()
because the syntax is rather confusing–it uses ahas:
key in its object argument, breaking off of the main locator call chain.If you’re scraping rather than testing, then I’d just use plain CSS as suggested in this answer.
That said, you may want to provide more context for your use case (full HTML tree, with containers) because there could be a better way to do this. If
row-id
s are unique, then they’re effectivelydata-testid
s, which can be queried directly, without thegetByRole
context query.