Refreshing an iFrame in a Web Browser Control - c#

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.

Related

How to assign HtmlDocument to a web browser, then interact with specific controls via GetElementById in c# / Console application

I have a requirement to navigate to a specific website, select a value from a drop down list, then click on a submit button. The solution has to be via a console app via c#.
So working backwards, I need to eventually be able to interact with controls like this:
html.GetElementById("shellmenu_0").InvokeMember("Click");
In order to work with html elements I need an HtmlDocument object. So as an example, the code below attempts to go to the Microsoft site and click a link to a new page. The code is as follows.
WebBrowser wb = new WebBrowser();
wb.AllowNavigation = true;
wb.Navigate("https://www.microsoft.com/en-us/?ql=4");
HtmlDocument document = wb.Document;
document.GetElementById("shellmenu_0").InvokeMember("Click");
I think I am on the right track, but I am hitting a null exception on the document. I must be missing a step to assign the website to the document somehow.
Any advice would be greatly appreciated. Thanks in advance!

Webbrowser, scripts and possible alternatives

I have a situation where a rather clever website updates the latest information on the site via Shockwave Flash through a TCP connection. The data received is then updated onto the page via JavaScript so in order to get the latest data a browser is required. If attempts are made to hit the website with continual requests then a) you get banned and b) you're not actually getting the latest data, only the last updated base framework.
So I need to run a browser with scripts enabled.
My first question is, using the standard WPF WebBrowser in .NET I get the following warnings which I don't get in standard IE, Chrome or Firefox. What is causing this and how do I supress/allow it but still allowing scripts for the site to be run?
My second question relates to is there a better way do to this or are there any better alternatives to the WebBrowser control that will
Allow scripts to run
can access the DOM or html and scripts returned in at least text format
is compatible with WPF
can hide the browser as I don't actually want it displayed.
So far I've looked into WebKit.NET which doesn't seem to allow access to the DOM and didn't like WPF windows when I tested and also Awesomium but again didn't appear to allow direct access to the DOM without javascript.
Are there any other options (apart from hacking their scripts)?
Thank you
set WebBrowser.ScriptErrorsSuppressed = true;
Ultimately I ended up keeping the WPF control and used this code to inject a JavaScript script to disable JavaScript errors. The Microsoft HTML Object Library needs to be added.
private const string DisableScriptError = #"function noError() { return true;} window.onerror = noError;";
private void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
InjectDisableScript();
}
private void InjectDisableScript()
{
HTMLDocumentClass doc = webBrowser1.Document as HTMLDocumentClass;
HTMLDocument doc2 = webBrowser1.Document as HTMLDocument;
IHTMLScriptElement scriptErrorSuppressed = (IHTMLScriptElement)doc2.createElement("SCRIPT");
scriptErrorSuppressed.type = "text/javascript";
scriptErrorSuppressed.text = DisableScriptError;
IHTMLElementCollection nodes = doc.getElementsByTagName("head");
foreach (IHTMLElement elem in nodes)
{
HTMLHeadElementClass head = (HTMLHeadElementClass)elem;
head.appendChild((IHTMLDOMNode)scriptErrorSuppressed);
}
}
WPF WebBrowser does not have this property as the WinForms control.
You'd be better using a WindowsFormsHost in your WPF application and use the WinForms WebBrowser (so that you can use SuppressScriptErrors.) Make sure you run in full trust.

How to update DOM content inside WebBrowser Control in 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.

How to refresh the parent window from child window close in asp.net

How to refresh the parent window from child window close in asp.net (.cs page). We are using
page.ClientScript.RegisterStartupScript(this.GetType(), "PopupSave", "<script> JavaScript(`successfully Saved`); window.close();window.top.opener.RefreshPage();</script>");
for refresh the parent page from child window close.
Here RefreshPage() is a custom function
function RefreshPage() {
window.document.forms[0].submit();
}
It is working fine in IE but not in Mozilla Firefox and Google Chrome.
Update
Again it is not working with your solution also. My project structure is entirely different then what you explained above. We are calling the Java script functions in code behind by using page.ClientScript.RegisterStartupScript(). Here we are calling the different JavaScript functions separated by semicolon(;). See below
page.ClientScript.RegisterStartupScript(this.GetType(), "PopupSave", <script>javascript:alert('Successfully Saved'); window.close();window.top.opener.RefreshPage();</script>");
here RefreshPage() is the user defined JavaScript function i.e.
function RefreshPage()
{
window.document.forms[0].submit();
}
It is working fine in Internet Explorer but not working in Mozilla Firefox and Google Chrome.
In Mozilla Firefox the pop up value is saving in the database but it is not updating into the parent page. If I did refresh manually the value is getting updating into the parent page.
If I put debugger in RefreshPage() function in IE it is firing but not in Firefox.
Perhaps you could use something like
window.parent.forms[0].submit();
or if you do not want to use the submit functionality you could use
window.parent.location.href = 'yourURL';
Maybe you should move the call to RefreshPage() before the window.close().
window.opener.location.href = window.opener.location.href;

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.

Categories

Resources