How to programmatically remove an HTML element from a web browser?

I am using a web browser control and the document is loading an HTML page. I want to remove an item programmatically from a document.

Can someone explain to me how to remove an item by id or attribute of name?

+4
source share
4 answers

You can accomplish this using the Microsoft.mshtml library. I executed it using a force of type dynamic :

 private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate("https://www.google.com/"); } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (e.Url.ToString() == "https://www.google.com/") { dynamic htmldoc = webBrowser1.Document.DomDocument as dynamic; dynamic node = htmldoc.getElementById("lga") as dynamic; node.parentNode.removeChild(node); } } 
+3
source
 webbrowser.Document.GetElementById("element").OuterHtml = ""; 
+6
source

This is the version of VB.Net. I tried to remove MsHTML. but the link to this library has its own problem. The following is not a direct answer, but can be a workaround to stop loading external resources using iframes

 For Each FrameElement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("iframe") Debug.Print(FrameElement.OuterHtml) FrameElement.OuterHtml = Nothing Next 
+1
source

OuterHtml Unable to change!

This code removes Css links from WebPage:

  Sub RemoveStylesheet() Dim styles As HTMLStyleSheetsCollection = WB.Document.DomDocument.styleSheets 1: If styles.length > 0 Then For Each stl As Object In WB.Document.DomDocument.styleSheets ' stl.removeImport(0) If stl Is Nothing Then Continue For Dim st As IHTMLElement = stl.owningElement ' st.href = "" ' MsgBox(st.tagName) st.parentElement.removeChild(st) Next GoTo 1 End If End Sub 
0
source

All Articles