There was a need to find elements by the code of the elements themselves, for example -

<a><img src="/bitrix/templates/mobile/images/google-play-img.png" alt="">test</a> 

Could not find anything like that.

Closed due to the fact that it is necessary to reformulate the question so that it is possible to give an objectively correct answer to the participants of the Air , Viktor Tomilov , cheops , kizoso , AK 1 Feb '18 at 18:59 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    the essence of the question is incomprehensible - Igor
  • Well, take all the elements and find the one whose src property is /bitrix/templates/mobile/images/google-play-img.png - torokhkun
  • What's not clear? you need to search for the dom element line and not for src and not for alt but for the whole line with all the attributes that may appear in it, or nested elements may appear, which also implies searching by the string. - Query
  • I think it is not advisable to go through all the parameters of all elements of html in order to find different combinations of properties. - Query
  • In general, you need a method or function that can find a dom-element by a string from its HTML code. - Query

2 answers 2

Well, it's easy. You first need to parse the string. So you will get a list of nodes and their parameters. And you will have to go over all elements of the document and compare the correspondence of parameters and descendants.

Parsing can be replaced by the creation of an element with this html structure. And compare all the nodes of the document with the nodes of this element.

  • It turns out you will need to pass all levels of nesting on the cycle on the page? But there is no search? - Query
  • You can try to take the document.innerHTML, this is the entire html of the document in the string. And look for the string in the string, get the starting position, then you can delete this part of the string. - Profesor08
  • I think this is the optimal algorithm for the speed of work, although the disadvantage is the complexity of implementation. - Dmitry Polyanin

You can do so.
We are looking for the first tag in the search bar, in the example above it is a .

Further through document.getElementsByTagName we take all tags a from the document, and we compare with a search line through outerHTML.

Updated:
First we insert the search text into the page (somewhere in a hidden place, after the search is unforgettable, we delete the element) and take outerHTML from it, and then we do what I wrote above.
Why is this necessary? The fact is that the browser stores the page in its format, and if you have an extra space, for example, or a line break, the algorithm above will not work. Even if there are two spaces in the source file, there will be one space inside the browser. Therefore, this action we bring the line to the browser view, if there is a need for it.

  • The fact that you see one space does not mean that it is there alone. And there are as many of them as they were originally, they are not going anywhere. And the fact that you see only one is only a feature of the browser render. The CSS property white-space: pre will show all spaces. - Profesor08
  • @ Profesor08 checked on a specific file, indeed in the line between tags, spaces and line breaks are preserved, but in the attribute part of the tag, the extra spaces and also line breaks are removed, at least in chrome. - Dmitry Polyanin
  • @ Profesor08 here is a test - jsfiddle.net/0qnt6kpt - Dmitry Polyanin
  • @ Profesor08 moreover, he can even transform tags, in the example above, the tag <br/> he converts to <br> - Dmitry Polyanin
  • Well, the attributes for that and invented, so as not to affect the content. And the tag does not need a closing tag. - Profesor08