Recently wrote a software called WebBrowser, the purpose of the Web page to automate the processing, that is, all of your actions on the Web page can be recorded in a script, and then you can replay the process. I mean, any process.
The program is written in C #, which encountered a problem, for the Web page, I was using C # built-in WebBrowser control control, but this control has a problem, for some of the links on the Web page, if you open in IE, it is opened in a new window, this time, You click in the WebBrowser, it will be silly not garbage open ie to browse, so, how do I control the new open page in my program Ah, really, must use all click of the page in my WebBrowser inside open, I can control its automated action processing.
However, such a coincidence, C # built-in WebBrowser is not mandatory to make this realization of the method, so Google at home and abroad, found that there is no good way, robust, stable method, alas, also have their own solution ...
Generally click on a Web page to open a link in a new window, this moment, Webbrower statustext will be the URL of the new link, this time, you can use Mainbrowser_newwindow message, use E. Cancel=true, take a message to open a new window action, and then use Webbrowser.navigate (just the URL), so you can not call IE to open a new window.
But sometimes, statustext is not the exact URL of a new window, and then WebBrowser will open the wrong page. Do not believe, understand the programming friend can try www.hao123.com above Baidu button, you get is www.baidu.com/s URL, but this is not the real new window URL, how to do?
In fact, the page will open the link in a new window, because the link has a property target set to blank, if we set it back to _self, then OK ...
In combination with the above analysis, my solution is as follows:
The code explains everything, as follows:
private void Wb_mainbrowser_newwindow (object sender, CancelEventArgs E )
{
E.cancel = true;
Try
{
string url = This.wB_MainBrowser.Document.ActiveElement.GetAttribute ("href");
THIS.WB_MAINBROWSER.URL = new Uri (URL);
}
Catch
{
}
}
Above is the first time to avoid, the following is the second analysis implementation, using the loaded back page of all the controls on the target property modification is also _self.
private void Setallwebitemself (htmlelementcollection items)
{
Try
{
foreach (HtmlElement item in items)
{
if (item. Tagname.tolower (). Equals ("iframe", StringComparison.OrdinalIgnoreCase) ==false)
{
Try
{
Item. SetAttribute ("Target", "_self");
}
Catch
{ }
}
Else
{
Try
{
HtmlElementCollection Fitems = Item. Document.window.frames[item. Name]. document.all;
This. Setallwebitemself (Fitems);
}
Catch
{ }
}
}
}
Catch
{
}
}
Of course, when the page is finally loaded, the above method is invoked, as follows:
Before complete, set the target of the control on the document to _self to avoid the new window becoming IE default Open
This. Setallwebitemself (THIS.WB_MAINBROWSER.DOCUMENT.ALL);
On how to judge WebBrowser final loading method, please see my another article "[Original]c# WebBrowser accurate Judgment page final load complete"
In this way, once the Web page has been loaded, all the control target has become _self, and later, you click on what links are changed in this page opened, under normal circumstances will not trigger Mainbrowser_newwindow, the first step, just in case.
The above method, already can use 90% of the link can be opened in this page, there are some flash, pictures link something, more special, there is a little shortage.
For ordinary, already can, do not believe you can try www.hao123.com Google's search results, and so on, the original in the IE new window, now open in the WebBrowser, hehe.