Ranorex: How get InnerText of element

Viewed 11

Can one get the innerText of an HTML element with a Ranorex adapter? By innertext I mean the text of the selected element and the text of all its descendants.

1 Answers

This post from the official Ranorex support implies that there is no function in Ranorex that can do this. Therefore this post advises to first get the inner HTML via GetInnerHtml() and then filter out the element tags in this string.

However, the suggested post falls short for one special case:

An HTML-comment contains HTML tags, e.g.

<!--<div>else42</div>-->

The regular expressions suggested in the post <.*?> would not filter the --> out. Therefore HTML-comments have to be filtered out before the HTML-tags are filtered out:

string innerText = webElement.GetInnerHtml();
            
if(innerText != null) {
  innerText = Regex.Replace(innerText, "<!--[\\s\\S]*?-->", string.Empty); // remove HTML-comments
  innerText = Regex.Replace(innerText, "<.*?>", string.Empty); // remove HTML-tags
}
Related