C# Winform Webbrowser not updating after document text update - c#

Good Morning,
I have a Web browser embedded within a C# winform. When loading the web-browser, it loads in a local file and displays the page with no issues.
I then have a button with an OnClick method which does the following:
private void button1_Click(object sender, EventArgs e) {
this.webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(String.Empty);
this.webBrowser1.DocumentText = //PathToDocumentText;
}
This was taken from this SO question and causes the web browser to freeze up. On hover shows the cursor with the loading spinning icon.
I am simply wanting to change the document text from one local file to another (both work if I load them in manually OnLoad).
Any help appreciated.

this.webBrowser1.Navigate("about:blank");
this.webBrowser1.Document.OpenNew(false);
this.webBrowser1.Document.Write(//pathtoFile);
this.webBrowser1.Refresh();
This does the trick, Thanks to anyone who looked at this.

Related

WebBrowser Control Retrieving jQuery Text

I am trying to retrieve whenever the website displays the following message from a jQuery event. Initially this HTML inst displayed in the HTML.
<div id="toast-container" class="toast-top-right"><div class="toast toast-error" aria-live="assertive" style="display: block;"><div class="toast-message">Check email & password.</div></div></div>
My assumption is, that the webBrowser1.DocumentText.Contains is only looking from the initial load of the content.
So I thought maybe some sort of timer would work every 5 seconds, looking to see if the code has changed - but I don't even think this is right as it's checking the code that's already loaded repeatedly?
private void timer2_Tick(object sender, EventArgs e)
{
// Checks for any errors on sign in page
if (webBrowser1.DocumentText.Contains("toast toast-error"))
{
// Toast Notifications
var signinErrorNotification = new Notification("Error", "Please check your email and password are correct.", 50, FormAnimator.AnimationMethod.Fade, FormAnimator.AnimationDirection.Left);
signinErrorNotification.Show();
}
}
How do I go about getting the latest code that's been affected by any jQuery.
P.S. My c# level is beginner.
The Document property should give you what you need.
Notice that the docs for DocumentText say
Gets or sets the HTML contents of the page displayed in the WebBrowser
control.
For Document they say
Gets an HtmlDocument representing the Web page currently displayed in the WebBrowser control.
To me that's saying that DocumentText is like the starting document and Document is the current DOM. Also see https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-access-the-managed-html-document-object-model

How to open url in new browser tab from Web browser control (displaying PDF having a link) c#

I am facing this issue, working on Active reports 9. Every thing is fine as per our application we generate the report and in UI user will be viewing in a c# Web browser control.
Now the issue am facing is when client(user) clicks on the link present in pdf i.e. on Web Browser control. With in the same window the link is opening. They want the link to open in new window.
The problem q=am facing is if its Html control i would have used target="_blank" property but not , and its a windows application i cant even use Java script. I just gone through the properties of Picture control used in Report, theres only Hyperlink property which states in pdf it converts it to href or a tag.
Need some assistance as soon as possible is that possible to do in web browser control or should change any properties for picture control in Code behind.
Hope this help you. It worked for me.
Add the Navigating event on your webBrowser control. This will open the link in a new Browser window. In my case Google Chrome.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
try
{
if (!(e.Url.ToString().ToLower().Contains("file") || e.Url.ToString().ToLower().Contains("pdf")))
{
e.Cancel = true;
//Open Link
Process.Start(e.Url.ToString());
}
}
catch (Exception err)
{
//Handle Exception
}
}

C# WebBrowser different html document after navigate

I have a really strange problem in C#:
First I use the WebBrowser control and the navigate method to browse.
wb_email.Navigate("https://registrierung.web.de");
Now I can change the innerText of htmlelements without any problems.
wb_email.Document.GetElementById("id4").InnerText = "Alexander";
But when I reload the page by simply using the navigate method with the same url again,
I get a null exception. It seems as he can't find the element.
So I used an inspector for Firefox to see if the htmlelement really changed, after reloading.
But only the url is changing, htmlelements are all the same.
What I'm doing wrong?
You're just changing the DOM in the displayed page. When you reload the page, the WebBrowser instance will just refresh the DOM from the server again and lose your changes.
The WebBrowser class isn't designed for editing rendered pages inside itself, as it's basically just a wrapper to an embedded Internet Explorer instance.
Make sure the website has finished loading before accessing any element. Like:
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Access elements here
}

How to grab the Contents Which updated by JavaScript WebBrowser

private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser web = (WebBrowser)sender;
richTextBox1.Text = web.DocumentText;
}
above is sample code.
it's giving all Text of Current Open, if contents is updated by JavaScript, it visible but Document.Text not update.
Please Help guys
I had the same problem. Use the following sample code:
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
string content = doc.body.innerText;
Also, add mshtml to the references of your project (if you dont know how to add the refernce, just google it).
Actually, whenever you use this code, the value in the doc variable is the updated version of the contents of the webbrowser.
Good Luck
I would guess that the javascript that is executing on the page which is modifying the content is happening after the DocumentCompleted event; Perhaps you can try a different event such as 'Invalidated'.
The WebBrowser.DocumentText also many not reflect any changes to the DOM, and you may need to navigate the DOM through the WebBrowser.Document property.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.document.aspx

How to render a string containing HTML into an image in C#?

I am developing a web application that has an interactive feedback tool for users. In this application users can click a send feedback button. The button puts up an overlay over their current web page and allows them to drag highlight area DIVs to emphasize certain areas. Once they submit their feedback the HTML of the entire page is passed via AJAX back to the server.
Once on the server I now have a string containing the HTML of the page. From here I would like to run this string through some sort of engine that renders the HTML and builds an image. A sort of round about way of taking a screenshot if you will.
How might one accomplish something like this? Are there engines available that are written in C# and can build up the HTML and render an image?
Check out this framework - http://awesomium.com/
This is exactly what you need.
Set the base URL, this will be needed to resolve any relative URLs
WebCore.SetBaseDirectory("C:\\MyApplication\\MyBaseDirectory");
Then load the HTML -
myWebView.LoadHTML("<p>Hello World!</p>");
Then use the .Render() method, and you'll be able to save the rendered content to an image.
You can consider usin LLMozLib if you want to go by Gecko.
See more details here
EDIT
There's an ActiveX control to embed Gecko on Windows.
Sample here
EDIT
I got it working on a Windows Forms application.
Using these resources.
It is a csharp wrapper to Gecko ...
That's my sample code ...
public partial class Form1 : Form
{
public Form1()
{
Xpcom.Initialize(#"C:\Users\esouza\Downloads\xulrunner"); //Tell where are XUL bin
InitializeComponent();
//geckoWebBrowser1 is an instance of GeckoWebBrowser control that I've dragged on the Form1
geckoWebBrowser1.DocumentCompleted += new EventHandler(geckoWebBrowser1_DocumentCompleted);
}
private void button1_Click(object sender, EventArgs e)
{
geckoWebBrowser1.Navigate("http://www.google.com");
}
void geckoWebBrowser1_DocumentCompleted(object sender, EventArgs e)
{
Bitmap b = new Bitmap(geckoWebBrowser1.Width, geckoWebBrowser1.Height);
geckoWebBrowser1.DrawToBitmap(b, new Rectangle { X = 0, Y = 0, Width = 800, Height = 600 });
b.Save("file.bmp");
}
}
Very interesting question and I've not got a silver bullet for you.
You're surely going to need a browser engine of some description and then capture the rendered output as a bitmap.
I see from this question that there was a COM wrapper developed for WebKit. Maybe that's a good starting point.

Categories