42

I am trying to use this one element:

cy.get('[data-cy-component=single-picker-search] input').type('Live');

When I run it, it tells me there are more than 1 of them, so it can't do it.

I tried adding { force: true } - that made no difference.

I tried looking at each element, but this fails if the element is not visible:

cy.get(singlePickerSearch).each(($el) => {
  cy.wrap($el).type('Live' + '{enter}');
});

How do I make it just type where the element is visible? I do not want it to fail on this.

2
  • Do you know which of input is visible during this test (e.g. first/third/last)? Commented Feb 7, 2019 at 13:45
  • No, I can only see only search text. Commented Feb 7, 2019 at 14:04

2 Answers 2

58

This didn't work for me on a button I was trying to get:

cy.get('[data-cy-component=single-picker-search] button:visible')

This is what ended up working for me:

cy.get('[data-cy-component=single-picker-search]').filter(':visible')

3
  • 2
    That filter should be in quotes, shouldn't it? Like this: .filter(':visible') Commented Feb 17, 2020 at 17:58
  • 1
    this worked for me too, the first one did not work (updated nov 2021)
    – Don Diego
    Commented Nov 23, 2021 at 17:07
  • docs.cypress.io/api/commands/filter
    – Ryan
    Commented Sep 7, 2023 at 15:59
34

Got it. You can use pseudo selector :visible so you will be able to do

cy.get('[data-cy-component=single-picker-search] input:visible').type(...)

or in case if more than one is visible select first visible input

cy.get('[data-cy-component=single-picker-search] input:visible').first().type(...)

Not the answer you're looking for? Browse other questions tagged or ask your own question.