2

In a C# winform application, I try to check if the innerText of an HtmlElement is white space or not.

 String.IsNullOrWhiteSpace(elem.InnerText)

The elem.OuterHtml is:

<div class="ad-container toplb">
 <span class="leader-board">
 <div id="div-gpt-ad-1409955032897-3">
 <script type="text/javascript">
  PbhAdUnit.cmd_push(function() {pbh_ad_units['div-gpt-ad-1409955032897-3'].display(); });
 </script>
 </div>
 </span>
</div

The content of innerText

  PbhAdUnit.cmd_push(function() {pbh_ad_units['div-gpt-ad-1409955032897-3'].display(); });  

I expect innerText to ignore scripts and return empty if there is no readable text (the text which appears on the page) inside the element. Anyway how can I ignore such elements?

Ahmad
  • 8,811
  • 11
  • 76
  • 141

1 Answers1

2

You can find the inner text of an element using InnerText property. It returns all inner text removing markup.

Sample Code:

private void Form1_Load(object sender, EventArgs e)
{
    this.webBrowser1.Navigate(@"d:\sample.html");
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var div = this.webBrowser1.Document.GetElementById("div1");
    MessageBox.Show(string.Format("'{0}'", div.InnerText), "InnerText");
    MessageBox.Show(string.Format("'{0}'", div.InnerHtml), "InnerHtml");
}

Sample Content:

Create a sample.html file using below content.

<html>
<head><title>Title</title></head>
<body>
    <div id="div1"><script type="text/javascript">alert('Hi');</script><input type="text"/></div>
</body>
</html>

Result:

I put '' around inner text and inner html:

enter image description here

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thank you, anyway I don't have this problem now as I remove scrip tags using the answer of this question http://stackoverflow.com/questions/13441470/htmlagilitypack-remove-script-and-style, but I expected you try my original html. – Ahmad Dec 25 '15 at 14:27
  • I also tried using your original html and the result is the same. But to keep the answer more general and more useful for future readers, I used above values for answer. You can simply put your desired content in this sample and see the result :) – Reza Aghaei Dec 25 '15 at 14:31
  • Thank you, I must debug it again. Actually, my problem has been with the `InnerText` itself. I will notify you. – Ahmad Dec 26 '15 at 12:52