How we handling dynamic Xpath

i’m facing difficulties handling dynamic Xpath. I have a radiobutton, and the status from xpath always changing when ever we open the page

ex.
//div[@id=‘status211259’]/label
//div[@id=‘status208477’]/label

These are just an example. How can i handle this? Please teach me, thank you

2 Likes

You could try to skip the number part and use “contains()”.
like this:
//div[contains(@id, ‘status’)]/label

If this is not uniqe enough, you could try to capture other attributes, like class, or text, and combine with the xpath above, but more info about the DOM is needed to be able to help you.

1 Like


here’s some DOM, is it helpful?
btw i’ve already try this method //div[contains(@id, ‘status’)]/label, but still not working

1 Like

i’ve already change the xpath become //label[@for=‘status’ and text()=‘Ok’]/input[@type=‘radio’ and @value=‘ok’]
but still not working

1 Like

When writing the xpath, can the element be found in the dom according to the written Xpath? You can use ctrl+f to search with xpath in devtools.
The first thing I see with this xpath is the label’s text is " Ok " and not “Ok”, the spaces matter.
I would first try to locate the element in dom with this xpath:

//label[text()=‘ Ok ’]

if this is a unique result (1 of 1) you can try putting /input or //input after it

//label[text()=‘ Ok ’]/input

im still can’t locate the element with those xpath

Generally, the id is supposed to be unique and should be enough to identify an object. However, there seems to be some exceptions (with certain app builders) such as for radio buttons. So, I think you can get your object by including the value attribute and the id, like:
//input[@id="status" and @value="ok"]

to get your other radio buttons, you would need to change the value attribute as per their respective “value”.
Also note that you do not need “contains()” on the id for the input element as the dynamic seems to be the <div> container, not the specific input element.

//div[contains(@id,‘status’)]/label/input[@value=‘ok’]
here’s the solution of this problem guys. Thank u for helping and giving insight to me

1 Like