Search code examples
c#webbrowser-controlxulrunnergeckofx

Remove whole div with specific class name


Is it possible to remove the whole div with a specific class name? For example;

<body>
<div class="head">...</div>
<div class="container">...</div>
<div class="foot">...</div>
</body>

I would like to remove the div with the "container" class.

A C# code example would be verry useful, thank you.


Solution

  • The proper way (I suppose) to do this is via built in Gecko DOM classes and methods.

    So, in your case something like:

    var containers = yourDocument.GetElementsByClassName("container");
    //this returns an IEnumerable of elements with this class. If you only ever gonna have one, you can do it like that:
    var yourContainer = containers.FirstOrDefault();
    yourContainer.Parent.RemoveChild(yourContainer);
    

    Obviously, you can also do loops etc.