I am developing browser automation for a website, using xPaths as the element selector for browser interactions. Whenever the website I am working on gets updated, the element xPaths change randomly. Is there any way to overcome this issue, or do I have to test and modify the browser action xPaths every time there is an update to the website?
Discussion posts and replies are publicly visible
May I know how you fetch the xpath , What i usually follow is right click on the position for which i want the xpath and select copy and i get the option to select full xpath . Would suggest to try this and see if any difference from your way
Hello Komal Jain ,
But wouldn't the full XPath change, when changes are made in the website?
Hi Rithani Vivekananthan & Komal Jain ,
Thanks for the reply.
The website has dynamic field. So it is using table layout.
The xPath i am getting is //*[@id="field_1359544387"]. Whenever there is any maintenance, the xPath id is changing. I couldn't find any pattern. It is random.
Then now I am trying the full xPath which is looks like /html/body/div[3]/div[2]/table/tbody/tr/td/div[1]/form/div[6]/table[8]/tbody/tr[208]/td/select. There is no maintenance after the changes.
I hope the full xPath will not change like xPath.
Hi Hirushiharan ,
Whenever possible, try to use unique attributes in your XPath expressions. Instead of relying solely on the position or structure of an element, use attributes like id or class that are less likely to change during updates. Utilize XPath functions to create more flexible and resilient expressions. Functions like contains(), starts-with(), and ends-with() can be helpful like below.
id
class
contains()
starts-with()
ends-with()
//div[contains(@class, 'partialClassName')]
//input[starts-with(@id, 'prefix')]
if you use full xpath, It is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.
Hi Sri Ram Kaja ,
Thanks for the information. Will try the functions.