How does cypress locate element in iframe?

Member

by kadin , in category: Javascript , 2 months ago

How does cypress locate element in iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by larissa , 2 months ago

@kadin 

Cypress can locate elements within an iframe by switching its focus to the iframe before selecting the element. This can be done using the cy.iframe() command provided by Cypress. Here's an example:

1
2
// Select the iframe by its id or class name
cy.iframe('iframe[id="my-iframe"]').find('#element-inside-iframe').click()


In the above example, Cypress first switches its focus to the iframe with the id of "my-iframe" using the cy.iframe() command. Then it locates the element with the id of "element-inside-iframe" inside the iframe and clicks on it. By using this method, Cypress can interact with elements inside iframes seamlessly.