The question is as given in the title, ie, to access element whose parent is hidden. The problem is that, as per the cypress.io docs :
An element is considered hidden if:
- Its width or height is 0.
- Its CSS property (or ancestors) is visibility: hidden.
- Its CSS property (or ancestors) is display: none.
- Its CSS property is position: fixed and it’s offscreen or covered up.
But the code that I am working with requires me to click on an element whose parent is hidden, while the element itself is visible.
So each time I try to click on the element, it throws up an error reading :
CypressError: Timed out retrying: expected '< mdc-select-item#mdc-select-item-4.mdc-list-item>' to be 'visible'
This element '< mdc-select-item#mdc-select-item-4.mdc-list-item>' is not visible because its parent '< mdc-select-menu.mdc-simple-menu.mdc-select__menu>' has CSS property: 'display: none'
The element I am working with is a dropdown item
, which is written in pug
. The element is a component defined in angular-mdc-web, which uses the mdc-select
for the dropdown menu and mdc-select-item
for its elements (items) which is what I have to access.
A sample code of similar structure :
//pug
mdc-select(placeholder="installation type"
'[closeOnScroll]'="true")
mdc-select-item(value="false") ITEM1
mdc-select-item(value="true") ITEM2
In the above, ITEM1
is the element I have to access. This I do in cypress.io
as follows :
//cypress.io
// click on the dropdown menu to show the dropdown (items)
cy.get("mdc-select").contains("installation type").click();
// try to access ITEM1
cy.get('mdc-select-item').contains("ITEM1").should('be.visible').click();
Have tried with {force:true}
to force the item click, but no luck. Have tried to select the items using {enter}
keypress on the parent mdc-select
, but again no luck as it throws :
CypressError: cy.type() can only be called on textarea or :text. Your subject is a: < mdc-select-label class="mdc-select__selected-text">Select ...< /mdc-select-label>
Also tried using the select
command, but its not possible because the Cypress engine is not able to identify the element as a select
element (because its not, inner workings are different). It throws :
CypressError: cy.select() can only be called on a . Your subject is a: < mdc-select-label class="mdc-select__selected-text">Select ...< /mdc-select-label>
The problem is that the mdc-select-menu
that is the parent for the mdc-select-item
has a property of display:none
by some internal computations upon opening of the drop-down items.
This property is overwritten to display:flex
, but this does not help.
All out of ideas. This works in Selenium
, but does not with cypress.io
. Any clue what might be a possible hack for the situation other than shifting to other frameworks, or changing the UI code?