Home > Back-end >  problem with find an element using cypress and xpath
problem with find an element using cypress and xpath

Time:05-31

I'm trying write a code (using cypress) to bypass long press verification such that verification image

this is inspect>> id and class values are changes every session <p id="oVzBRZgfQmDATFj" style="animation: 111.8ms ease 0s 1 normal none running textColorIReverse;">Press &amp; Hold</p>

I was tried XPath Selectors and i get the following: //p[text()='Press & Hold']

//p[starts-with(text(),'Press & Hold')]

//p[position()=1]

//p[normalize-space()='Press & Hold']

//p[last()]

//p[contains(text(),'Press & Hold')]

//p[contains(normalize-space(),'Press & Hold')]

//p[.='Press & Hold']

After experiment each of them such as element:

cy.xpath('//p[starts-with(text(),"Press & Hold")]').trigger('mousedown')
cy.wait(10000)
cy.xpath('//p[starts-with(text(),"Press & Hold")]').trigger('mouseup')

This error appears: Timed out retrying after 4000ms: Expected to find element: //p[starts-with(text(),"Press & Hold")], but never found it. error image

CodePudding user response:

If you don't have access to modifying selectors in the app, an easier approach would be to use .contains().

Assuming your <p> contains the events needed to verify, you can simply get the jquery element with the following:

cy.contains('p', 'Press & Hold')

I'm not sure if the verification process requires the user to be holding the click for 10 seconds, but i'm sure you can do less time.

If the .trigger() is not verifying, which could be guarded against, you can try using .realClick() from the cypress-real-events plugin.

CodePudding user response:

The code you have used succeeds in a simple test.

Using the following in a simple (isolated) test works

<p>Press &amp; Hold</p>
cy.xpath('//p[starts-with(text(),"Press & Hold")]').trigger('mousedown')  // passes

Since the purpose of the button is to check for automation, it's likely there's an <iframe> around it to try and avoid the automation tool (Cypress) from finding it.

  • Related