How to update DOM content inside WebBrowser Control in C#? - c#

I've a windows .Net Form which contains a WebBrowser Control.
This WebBrowser displays a webpage based on its Url property.
Can I modify the DOM of the displayed page inside the WebBrowser control ?
If yes, how to ?

For those who are interested, here's the solution:
HtmlElement headElement = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptElement = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement domScriptElement = (IHTMLScriptElement)scriptElement.DomElement;
domScriptElement.text = "function applyChanges(){/*DO WHATEVER YOU WANT HERE*/}";
headElement.AppendChild(scriptElement);
// Call the nextline whenever you want to execute your code
webBrowser1.Document.InvokeScript("applyChanges");

From http://msdn.microsoft.com/pt-br/library/system.windows.forms.webbrowser.aspx:
You can also manipulate the contents of a Web page through the Document property, which contains an HtmlDocument object that provides managed access to the HTML document object model (DOM) for the current page. This property is useful, when used in combination with the ObjectForScripting property, to implement two-way communication between your application code and dynamic HTML (DHTML) code in a Web page, letting you combine Web-based controls and Windows Forms controls in a single user interface. You can use the Document property to call scripting code methods from your application. Your scripting code can access your application through the window.external object, which is a built-in DOM object provided for host access, and which maps to the object that you specify for the ObjectForScripting property.

Related

How to click a link element programmatially with HTMLElement?

I'm doing an automation program. I load a webpage into my windows form and load it in WebBrowser control. Then, I need to click on a link from the WebBrowser programatically. How can I do this? for example:
Google Me
Facebook Me
The above are 2 different conditions. The first element does not have an id attribute while the second one does. Any idea on how to click each programmatically?
You have to find your element first, by its ID or other filters:
HtmlElement fbLink = webBrowser.Document.GetElementByID("fbLink");
And to simulate "click":
fbLink.InvokeMember("click");
An example for finding your link by inner text:
HtmlElement FindLink(string innerText)
{
foreach (HtmlElement link in webBrowser.Document.GetElementsByTagName("a"))
{
if (link.InnerText.Equals("Google Me"))
{
return link;
}
}
}
You need a way to automate the browser then.
One way to do this is to use Watin (https://sourceforge.net/projects/watin/). It allows you to write a .Net program that controls the browser via a convenient object model. It is mainly used to write automated tests for web pages, but it can also be used to control the browser.
If you don't want to control the browser this way then you could write a javascript that you include on your page that does the clicking, but I doubt that is what you are after.

Refreshing an iFrame in a Web Browser Control

C# Visual Studio 2010
I have a complex webpage that contains several iframes that I am loading into a web browser control. I'm trying to figure out a way to refresh one of the iframes when a user clicks a button on the windows form.
I can't find anything specific to refreshing a single iframe. Any ideas?
From within the DOM, you can just invoke:
document.getElementById([FrameID]).contentDocument.location.reload(true);
Using the WebBrowser control, you can execute javascript yourself, by using the InvokeScript method of Document:
browser.Document.InvokeScript([FunctionName], [Parameters]);
Put these two concepts together by writing your own function in the parent page:
function reloadFrame(frameId) {
document.getElementById(frameId).contentDocument.location.reload(true);
}
And invoke this in your C# code:
browser.Document.InvokeScript("reloadFrame", new[] { "myFrameId" });
How about using MSHTML and the reload method of the IHTMLLocation interface. You would add a reference to Microsoft.mshtml then try:
IHTMLDocument2 doc = webBrowser1.Document.Window.Frames["MyIFrame"].Document.DomDocument as IHTMLDocument2;
IHTMLLocation location = doc.location as IHTMLLocation;
if (location != null)
location.reload(true);
A value of true reloads the page from the server, while false retrieves it from the cache.

Set JavaScript events in WebBrowser control

I have a WebBrowser control and try to set onclick and href attributes on all links.
foreach (HtmlElement link in webBrowser1.Document.Links)
{
link.SetAttribute("href", "http://www.google.com");
link.SetAttribute("onclick", "return false;");
}
It works well. When i out source code of outer html i see that attributes was exist.
But JavaScript code does not work. Why and how i can force WebBrowser control to execute javascript code?
onclick is a property of type IDispatch. You can assign a function with no parameter to the property or add an attribute to HTML and let the parser to do this for you but you can't assign a string to it via DOM.
You can either pass an object that implements IDispatch and the default method (dispid=0) to the property, or use IHTMLElement2::attachEvent to attach an event handler. If you are using Microsoft's class libraries, you can use Microsoft's wrappers like HtmlElement.AttachEventHandler in Windows Forms and HtmlElement.Attachevent in Silverlight.

Windows Forms WebBrowser control and iframes

I am trying to create a piece of software to more or less automate form-fillings on a webpage - and I have decided to use the WebBrowser control in System.Windows.Forms. This works great and I can easily manipulate the DOM through webbrowser.Document and so forth. However unfortunately the site that I am doing automation on has a file upload which is ran inside an iframe - and this is where I am stuck, I simply cannot work out how to be able to manipulate elements inside the DOM of the iframe.
Ideally what I'd like to do is something like:
HtmlElement iframe = browser.Document.GetElementById("iframe_id");
iframe.InnerDocument.GetElementById("file_upload_input").SetAttribute("value", "myfile.txt");
And then submit the form inside the iframe of course - however there is no InnerDocument attribute on HtmlElement as far as I can see, nor no type that I have found that I can cast HtmlElement to so that I can access the inner DOM.
How this is done?
Try using the "frames" collection instead. From MSDN:
The iframe element functions as a
document within a document, or like a
floating frame. The frames collection
provides access to the contents of an
iframe. Use the frames collection to
read or write to elements contained in
an iframe. For example, the syntax for
accessing the backgroundColor style of
the body object in an iframe is:
sColor =
document.frames("sFrameName").document.body.style.backgroundColor;

ASP.NET dynamically insert code into head

I'm working inside of a Web User Control (.ascx) that is going to be included in a regular web form (.aspx), but I need to be able to dynamically insert code into the head of the document from the User Control. In my Coldfusion days <cfhtmlhead> would do the trick. Is there an equivalent of this in ASP.NET or a similar hack?
To add HTML markup you can do the following:
In your UserControl's code you can access Page.Header, which is itself a control. To that control you can then add new controls:
HtmlGenericControl newControl = new HtmlGenericControl("someTag");
newControl.Attributes["someAttr"] = "some value";
Page.Header.Controls.Add(newControl);
To add script markup you don't need access to the head tag at all since ASP.NET has helper methods on the ClientScriptManager that do the work for you:
Here are examples of some code you can also put in your user control's code:
// Register some inline script:
Page.ClientScript.RegisterClientScriptBlock(GetType(), "myAlertScript", "alert('hello!')", true);
// Register a script reference:
Page.ClientScript.RegisterClientScriptInclude(GetType(), "myLibraryScript", "~/Scripts/MyScriptLibrary.js");
I realize that this is an old question, but this is another example.
Try This:
Page.Header.Controls.Add(
new LiteralControl(
"<script>alert('Literal Added to <Head>.');</script>"
)
);
If you want to add the script at a particular index of the <head> you can use
AddAt(index, new LiteralControl(...)) where index 0 equals the top of the <head>
Also, you need to add runat="server" in your head tag e.g. <head id="head1" runat="server">
this.Page.Header.Controls.Add
By doing this, you are adding controls to the head section. You can add any type of control. If you feel you need to add simple text (or you want to write the tags manually), then look into the LiteralControl class.
There's some guidance on using C# code to modify the page header here. It should work just fine from any server-side code that executes before the page load completes.
A simple e.g.
HtmlHead head = Page.Header;
HtmlTitle title = new HtmlTitle();
title.Text = "Test Page";
head.Controls.Add(title);
HTMLHead reference is in namespace
System.Web.UI.HtmlControls
Override the custom control's Load() method to add the controls or references you need into the page header while the parent .aspx page is being loaded server-side.
I have a simple solution for this. Create a runtime memory cache based on the url of the page (as a key) that holds x information about y (be it a file reference, script text, or class that generates JavaScript) and serialize its data to JSON. Newtonsoft is helpful for instances of any class. In fact, you can use it's output to initialize any new instance of a class based upon given input. In a way, that means you may have your instances of any particular class automatically instantiated despite what user control the instance is on. In the end, you create a simple web form to serve as a script reference and as the final endpoint. It pulls the JavaScript (or what've it) and spits out the client side code you need as a script reference inside the head tag.

Categories

Resources