getText()
retrieves the text between the start and end tags of an element, while getInnerHtml()
fetches the HTML syntax of the element.
Let's consider an example HTML snippet and apply both actions on the a
tag:-
<a><span>link text</span></a>
getInnerHtml
:
This falls under the DOM Parsing and Serialization Specification.
getText()
:
Explanation : When using getText()
, it will only return the exact text, such as link text
. On the other hand, getInnerHtml()
will provide the HTML syntax, for example
<span>link text</span>
.
This ultimately depends on the specific requirements.
If the goal is to extract text along with HTML syntax for HTML parsing, then getInnerHtml()
should be employed.
If the objective is solely to retrieve the element's text content, then getText()
is preferred.
However, there have been cases where getText()
failed to capture the text due to design complexities in the HTML structure or other factors. In such scenarios, one can resort to using getAttribute('textContent')
.
In situations like this, instead of acting on the a
element, utilizing getInnerHtml()
to obtain the text from the span
element would be more suitable.
Note: getInnerHtml()
can also be accessed through getAttribute("innerHTML")