2

I'd like to know a way of removing elements of a site on a webbrowser of windows forms. I have this code:

namespace Browser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           InitializeComponent();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Document.GetElementById("ads").Style = "display:none";
            webBrowser1.Document.GetElementById("navigation").Style = "display:none";
            webBrowser1.Document.GetElementById("donate").Style = "display:none";
            webBrowser1.Document.GetElementById("social_bookmarking_buttons").Style = "display:none";
        }
    }
}

As you may notice, i'm just hiding the elements and I want to remove them. Thanks for your time.

David Amaral
  • 131
  • 1
  • 12

1 Answers1

3

This works for me to remove the elements.

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.GetElementById("ads").OuterHtml = "";
    webBrowser1.Document.GetElementById("navigation").OuterHtml = "";
    webBrowser1.Document.GetElementById("donate").OuterHtml = "";
    webBrowser1.Document.GetElementById("social_bookmarking_buttons").OuterHtml = "";
}
jhmt
  • 1,401
  • 1
  • 11
  • 15
  • Thanks, that's working. But I have a question, this site that I'm working on has a notice that only appears 5 seconds after opening the site. If I remove it when Document is Completed the notice is not removed. Can you help on this one? – David Amaral Oct 18 '15 at 13:18
  • If I were you, I'll use a global `System.Windows.Forms.Timer` object to notify 5 seconds has been past. I'll start the time in `DocumentCompleted` and check if the notice is not removed and then remove the elements in `Timer.Tick` event handler. – jhmt Oct 18 '15 at 20:12
  • Thanks. I already had resolved that problem. Can you help me on this question?: http://stackoverflow.com/questions/33199399/changing-the-position-of-html-elements-in-windows-forms-webbrowser. – David Amaral Oct 18 '15 at 20:13