Reputation: 89
Here is the code that I am using
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
IHTMLDocument2 doc2 = webBrowser1.Document.DomDocument as IHTMLDocument2;
StringBuilder html = new StringBuilder(doc2.body.outerHTML);
String substitution = "<span style='background-color: rgb(255, 255, 0);'> sensor </span>";
html.Replace("sensor", substitution);
doc2.body.innerHTML = html.ToString();
}
It works, but the I cannot use the form nor the web browser
I have tried to added
webBrowser1.Document.Write(html.ToString()); //after doc2 at the end
But the webpage displayed is not formmatted correctly
I would be grateful, to get this fixed
Upvotes: 0
Views: 619
Reputation: 6103
You first need to find your element in the HTMLDocument
DOM and then manipulate the innerHTML
property with the relevant HTML.
There are a variety of ways to do this, including injecting javascript (here) or using HtmlAgilityPack.
The following code uses GetElementsByTagName DOM function to iterate over the span
tags in the document on this site: https://www.w3schools.com/html/
It replaces all span text's including "Tutorial" with the html snippet your provided.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var elements = webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement element in elements)
{
if(string.IsNullOrEmpty(element.InnerText))
continue;
if (element.InnerText.Contains("Tutorial"))
{
element.InnerHtml = "<span style='background-color: rgb(255, 255, 0);'> sensor </span>";
}
}
}
Upvotes: 1