Find specific html element if it has no ID

180 Views Asked by At

I currently have an application where users can highlight a specific html element on any website. User A can share this element with the other user, where user B will open the site and see that element highlighted. The problem is how to find the element that has been highlighted on the site if it has no ID. I can copy its outerHTML and then find element by that, but I am not sure that will always work, what if a few elements have the same outerHTML.

1

There are 1 best solutions below

2
Throvn On

I have the feeling that you are looking for XPath. XPath is basically the unique path to your DOM element. You can read more about it here.

Examples of XPaths:

//*[@id="question"]
/html/body
/html/body/div[3]/div[2]/div/div[1]/div[3]/div[1]/div/div[3]

You can get the XPath on any webpage by opening up the Inspector (in the developer console), right clicking the desired element, choosing copy and then XPath from the dropdown menu.