Ranorex: How get InnerText of element

707 views Asked by At

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

There are 1 answers

0
else42.de On

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.

1.) 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.

2.) Also, script elements should be removed.

So the extended version of the code is:

string innerText = webElement.GetInnerHtml();
            
if(innerText != null) {
  innerText = Regex.Replace(innerText, "<!--[\\s\\S]*?-->", string.Empty); // remove HTML-comments
  innerText = Regex.Replace(innerText, @"<script[^>]*>[\s\S]*?</script>", string.Empty); // remove Scripts: https://stackoverflow.com/a/19414886/1777526            
  innerText = Regex.Replace(innerText, "<.*?>", string.Empty); // remove HTML-tags
}