0

I am unable to figure out why the cypress xpath is not working with the type(). I have two command functions: one that looks for the element using cy.get() and one that uses cy.xpath(). Unfortunately, this is a dynamic field so I have to use xpath.

(https://i.stack.imgur.com/wXl9q.png)

This is how I am using the above command.

(https://i.stack.imgur.com/snVn7.png)

Error:

(https://i.stack.imgur.com/08G32.png)

I tried to reading the cypress docs and searching on the internet, however the examples for solutions did not work. I am on on Electron version: 21.0.0, Bundled Node version: 16.16.0

Eva
  • 1
  • 1

2 Answers2

2

I think you are mistaken about the results of your xpath expression.

You have used //div[5]/div[1]/input which can return multiple elements.

The // predicate will "select all" if they are present.

Only / predicate is guaranteed to return a single element.

Since Cypress is telling you it found multiple elements, it is more likely that your selector is wrong than the Cypress library.

You will have to change the xpath selector.

Kan.Uranga
  • 21
  • 2
  • I get what you are saying. However, that is the same xpath that I use in my C# project and it works just fine. It is a condensed version of using for example: /html[1]/body[1]/div[3]/div[1]/div[2]/form[1]/div[1]/div[3]/div[3]/div[1]/div[1]/label[1], when instead I can take off what is the same and condense it to //form[1]/div[1]/div[3]/div[3]/div[1]/div[1]/label[1]. Thanks! – Eva Jan 12 '23 at 20:42
0

The answer is in your error message - you're trying to use cy.type() but the previous command (cy.xpath()) is yielding more than one element. I would focus on figuring out what differentiates the field you want to type in from the others found by cy.xpath(). If there is nothing different between them, then you can simply select the correct element by the 0-index of the element.

cy.xpath(element).eq(0).type(value); // assumes the first element yielded by cy.xpath
agoff
  • 5,818
  • 1
  • 7
  • 20
  • Thank you agoff. However, I did try the eq(0) and I received an error. I am using an absolute xpath to this element so I am not sure why cypress would think there are more than one elements with that address. Here is the error I got: The command that returned the promise was: > cy.type() The cy command you invoked inside the promise was: > cy.xpath() – Eva Jan 12 '23 at 01:13