TestProject Forum

Cant locate the element as xpath keep changing

Hi, I find out some buttons xpath might be changed everytime. like this time it is /html/body/div/div[5]/div/nz-modal/div/div[2]/div[1]/div/div/div/div[2]/button. but next time i run the test case, it will become /html/body/div/div[6]/div/nz-modal/div/div[2]/div[1]/div/div/div/div[2]/button, then the button cant be triggered.

Could I locate the element by attribute (e.g. class=“ant-btn ng-star-inserted ant-btn-primary”), or the name shown in frontend? Could is there any step that i could place a ‘click’ on a fixed position on the web, instead of an element? Thank you so much

Hi. Try replacing the problem area with ‘div[last()]’. I am testing an application where the div numbers update dynamically, but this has resolved it.

Also, I would recommend using the text or title attribute of that button.

Are your developers able to add an id to the element? That might make testing easier for you.

One more way you can try is, using parent method with relative xpath, i.e. //*[contains(text(),‘OK’)]/parent::button

Thx for all your replies, but seem were not applicable on my situation. Anyway, I found a way to do it.
I only put the class with those xpath number for locators. although there may be more than one matches, so far it can fulfill my requirement. Hope this sharing could also help you.