I am going to a URL in the form web browser. Once it loads I want to remove some elements and show a modified version to the user. My code compiles but I can't get the web browser in the form change what it is displaying. Been trying everything... I can't save page to html and then load that in the web browser because the online web page has an .aspx application I would like to use.
In this example I am trying to remove all <p>
html paragraph and one div with ID ContentPlaceHolder1_UpdatePanel1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace toolBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
dynamic htmldoc = webBrowser1.Document.DomDocument as dynamic;
dynamic node = htmldoc.getElementTagName("p") as dynamic;
node.parentNode.removeChild(node);
var htmlElement2 = webBrowser1.Document.GetElementById("ContentPlaceHolder1_UpdatePanel1");
htmlElement2.InnerText = "";
}
}
}
Similar to ASP.NET Web Forms, I don't think you could rely on _DocumentCompleted event to modify the HTML code. According to Microsoft, this event is described as (link)
Occurs when the WebBrowser control finishes loading a document.
This means that this event is fired when everything is ready to be rendered, at this momemnt, you won't be able to update the document from the code behind. Better approach would be using Javascript with equivalent logic to modify the document. If you are working with pure Javascript, consider this article for firing the "DOMContentLoaded" event