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.

Can't click on one of multiple elements returned by findAll

See original GitHub issue
  • Cypress Testing Library version: 5.0.2
  • node version: 12.0.0
  • yarn version: 1.15.2

Relevant code or config:

cy.findAllByTestId("droppable").then(result => {
      // Convert to array because the result is a jQuery object, tried it as well without converting
      const resultArray = Array.from(result);
      resultArray[1].click({ force: true });
    });

What you did:

My issue is that I can’t click on a single element returned by findAllByTestId. In my console, I can see that the query resolves to 2 DOM elements. When clicking either the first or the second, I see that the findAllByTestId query is executed, but the click command is not.

If I try clicking a nonexistent element from the array like resultArray[2], I do get an error that undefined cannot be clicked.

For context, the following does sort of work:

cy.findAllByTestId("droppable").click({multiple: true})

Using this snippet, I see that the first element was indeed clicked. As a result, both elements disappear and therefore I get an error that Cypress can’t click the second. However, I don’t want to click all elements, I want to click on just one of them.

What happened:

All commands in my test are executed except the click command (32 is findAllBy, 33 should have been the click command but is the next command).

image

Reproduction:

I tried reproducing but I couldn’t get the Cypress suite to work in CodeSandbox.

Problem description:

See above.

Suggested solution:

I might be overlooking something but I don’t find the docs very helpful at the moment: it lists one example of the combination of findAll with a user event, which I don’t even think is valid code:

cy.findAllByText('Jackie Chan').click()

click() should in this case probably be click({multiple: true}) but an example showcasing my issue would be even more helpful I believe.

Thanks in advance! Hope I posted in the right place because I believe this has to do with the Cypress implementation and not with the findAll API.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
mbaranovskicommented, Jan 12, 2020

@FlopieUtd Have you tried it with .eq(nthItem)?

Ref. https://docs.cypress.io/api/commands/eq.html#Syntax

7reactions
VasilyShelkovcommented, Nov 24, 2019

If for some reason you do not want to go with the solution by @kentcdodds and you are still keen on using the All variant is to use Cypress’ cy.spread which then allows you to click on one of the elements found. e.g.

cy.findAllByText('Jackie Chan').spread((firstButton, secondButton) => {
    secondButton.click()
})

There are probably other ways to do this, I think a good way to think about findAllBy* queries, is what you would do in Cypress if you used cy.get and it returned an array. You could use the other operators like cy.each and cy.first, although I haven’t tested these and only used cy.spread myself

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to locate element - multiple element seem to have ...
Usually this happens, when there are components with similar name/tags. Can you try: browser.
Read more >
Locating multiple elements in Selenium Python - GeeksforGeeks
One can locate multiple elements in 7 different ways. ... “xpath”), All elements with xpath syntax matching the location will be returned.
Read more >
ByRole | Testing Library
It can be used to query a specific element if multiple elements with the same role are present on the rendered content.
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 >
Selectors — Scrapy 2.7.1 documentation
.get() always returns a single result; if there are several matches, ... can select text or attribute nodes using CSS3 pseudo-elements:.
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