I use a web browser control and the document is loaded with an HTML page. I want to remove an element programmatically form the document.
Can any one guide me how can I remove any element by ID or name attribute?
I use a web browser control and the document is loaded with an HTML page. I want to remove an element programmatically form the document.
Can any one guide me how can I remove any element by ID or name attribute?
webbrowser.Document.GetElementById("element").OuterHtml = "";
You can accomplish this using the Microsoft.mshtml
library. I accomplished it using the power of the dynamic
datatype:
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);
}
}
It is the VB.Net version. I tried removing MsHTML
. but referencing that library has its own problem. The below is not a direct answer but can be 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
OuterHtml Can not be modified!
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