简体   繁体   中英

Loading page to WebBrowser control while skipping HTML elements by ID?

I am trying to load a certain page though WebBrowser control while avoiding unnecessary advertisement banners which load in DIV element called 'tb'.

How can I do this? I have done some googling and found an example using mshtml reference but I can't make it work from this example: https://stackoverflow.com/a/1218875

Any ideas?

Why wouldn't this work?

using System;
using mshtml;
using System.Windows.Forms;

namespace Client
{
    public partial class Client : Form
    {
        public Client()
        {
            InitializeComponent();

            HTMLDocumentClass htmldoc = wbBrowser.Document.DomDocument as HTMLDocumentClass;
            IHTMLDOMNode node = htmldoc.getElementById("tb") as IHTMLDOMNode;
            node.parentNode.removeChild(node);
        }
    }
}

I get an error:

'mshtml.HTMLDocumentClass' does not contain a definition for 'getElementById' and no extension method 'getElementById' accepting a first argument of type 'mshtml.HTMLDocumentClass' could be found (are you missing a using directive or an assembly reference?)

And:

Interop type 'mshtml.HTMLDocumentClass' cannot be embedded. Use the applicable interface instead.

You can do this using:

IHTMLDocument3 htmldoc = wbCtrl.Document.DomDocument as IHTMLDocument3;
IHTMLDOMNode node = htmldoc.getElementById("xBar") as IHTMLDOMNode;
node.parentNode.removeChild(node);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM