Unique identifier of the item for reference later

I am trying to figure out a method for storing a unique link to each tag on a specific page. I will not be able to edit the contents of the page, and I generated a UID to stay the same on every page.

Since browsers do not generate any UID for elements, I thought that the only way to do this would be to execute a script that goes over the DOM and creates a UID for each of them. I do not know how accurate this will be, especially considering that I will need to ensure that it will create the same UID for the tag every time the script crawls the page.

Can anyone think of other, more accurate ways to display the page?

Many thanks.

+4
source share
2 answers

I need the same functionality. The idea I had was to look at the location of the tag relative to a fixed element, such as the BODY tag, and use an XPATH expression as a unique identifier. For example, if there is HTML, for example

<BODY><TABLE><TD> 

... etc., the unique identifier for TD can be / Body / Table / 1 ... and so on. But this suggests that the next time the page is displayed, there will be no more nodes that were before. A slight improvement may be to use the "ID" tags in the path when they are generated, rather than using them where not. For example, suppose the page:

 <BODY> <DIV id="test"> <TABLE id="testtable"> <TR><TD></TD></TR> ..... 

The unique identifier for the TD tag can be / Body / Div @ test / Table @ testtable / TD @ 0, etc.

+2
source

If the content of the page remains unchanged between updates, then the obvious way is to simply generate the UID at the element position in the DOM. This should not even be an XPath expression; prime integer. However, if the content can change between updates, the task becomes much more difficult (if not impossible).

+2
source

Source: https://habr.com/ru/post/1304970/


All Articles