Reputation: 129
I am learning Cypress and have been automating various websites for the last few weeks. However, I am having issues clicking a button based on it's class or text value. (Supposed to be easy, I know)
The button does not have an id and has a long class name:
message-component message-button no-children focusable sp_choice_type_11 last-focusable-el
The following image shows the full element:
I have tried the following code:
cy.contains('Accept All').click()
cy.get('#message-component message-button no-children focusable sp_choice_type_11 last-focusable-el').contains('Accept All').click();
cy.get('button').contains('Accept All').click()
However, it just gives the same error and cannot find the button.
I have already checked the documentation for cypress and followed some advice off this post:
I also thought it may be that it loads the button after 4 seconds, so i upped the "defaultCommandTimeout"
to 7000. However, still no luck.
Does anyone have any ideas?
Much appreciated!
*** EDIT ****
Image of the error
Screenshot showing popup in cypress:
Upvotes: 0
Views: 2535
Reputation: 18650
The 'Accept All' button is inside an iframe. So you have to first go inside the iframe and then perform the click.
Install the cypress-iframe plugin using the command npm install -D cypress-iframe
Go to cypress/support/commands.js
and write import 'cypress-iframe';
In your test write:
cy.iframe('[title="SP Consent Message"]').find('button[title="Accept All"]').click()
Upvotes: 1