Unable to differentiate several objects with same attributes

Hello,

I’m facing an issue to identify several test objects that share the same attributes.
I’m testing an Angular web app.

I have a web page containing several fields named, for e.g., “Name” or “Birth date”.

I cannot identify those fields with the XPath as all of those fields do not appear at each execution (so the XPath changes depending on witch elements are displayed).

The IDs are dynamic and change at runtime (in the below example: “mat-input-20”).

I would like to identify the object at the bottom of the below screenshot:

The problem is that there are several objects with the following attributes on the page: tag=“input” and placeholder=“Nom *”

Is it possible to identify this object using a parent object?
In my case, this would be: tag=“h2” and text=“ENFANTS N°1” (at the top of the screenshot).

Thanks in advance for your help :slight_smile:

Yes it is possible or I should say it is generally possible. I just don’t know if Angular accepts the xpath. I also don’t know what is the symbol as part of your text for No1?

Here is a possible pathway.

//h2[text()="ENFANTS N°1"]/following-sibling::n9-adhesion-beneficiare/div[1]/form[1]/mat[2]/div[1]/div[2]/input[1]

Starting at the <h2> tag, move down (following; moving up is preceding) at the same (sibling) level to the n9-blah-blah, then move down the children chain (find the first child div, then its first child form, then the second child mat tag, then first div, then use the second child div) to the input. See if it works.

Edit: you can sometimes remove a child tag by using double slashes, like //, as long as the pathway remains unique. As well, you can also remove the child position (e.g. [2]) as long as the pathway remains unique. Note that [2] can also be written as [position()=2]

2 Likes

Hi grylion54,

thanks a lot for your help! I’ll try the suggested corrections and keep you informed.

Have a nice day,
Etienne

Thanks again, this is working!

I finally used this XPath expression:

//h2[text()="ENFANTS N°1"]/following-sibling::n9-adhesion-beneficiaire/div[1]/form[1]/mat-form-field[2]/div[1]/div[1]/div[1]/input[1]