Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  82] [ 3]  / answers: 1 / hits: 12376  / 3 Years ago, sun, march 28, 2021, 12:00:00

I want to know how to check if an element exists in the DOM of a web page in Cypress.


What would the equivalent of this piece of code in Selenium be in Cypress:


Boolean Display = driver.findElement(By.xpath("locator")).isDisplayed();

More From » selenium

 Answers
6

To query with an xpath locator, install the cypress-xpath extension.



Install with npm

npm install -D cypress-xpath


Install with Yarn

yarn add cypress-xpath --dev



In the test


cy.xpath(locator)            // driver.findElement(By.xpath("locator"))

Add a visibility check as well,


cy.xpath(locator)            // driver.findElement(By.xpath("locator"))
.should('be.visible') // isDisplayed()

or


cy.xpath(locator)            // driver.findElement(By.xpath("locator"))
.should('not.be.hidden') // isDisplayed()

[#1577] Tuesday, March 23, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mira

Total Points: 460
Total Questions: 108
Total Answers: 99

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
mira questions
;