C # webbrowser force the new window webpage to be opened only in webbrowser

Source: Internet
Author: User
Recently I wrote a software called webbrowser for Automatic Webpage operations. That is to say, all your operations on the webpage can be recorded in a script, then, you can play back the operation .. I mean any process.

ProgramI wrote it in C #, and encountered a problem. For webpages, I used the built-in webbrowser control in C #, but this control has a problem, for links on some web pages, if you open the link in a new window when you open IE, if you click in webbrowser, it can open ie without spam. In this case, how can I control the newly opened webpage in my program, you must use all the clicked web pages in my webbrowser to enable automatic action processing.

However, with such a coincidence, the built-in webbrowser of C # does not force this implementation method, So Google has gone abroad and found that there is really no good way, robust, and stable, alas, you have to solve it yourself...

Generally, a link opened in a new window is clicked on a webpage. At this time, the statustext of webbrower will be the URL of the new link. At this time, you can use mainbrowser_newwindow message and E. cancel = true, take the message to open a new window, and then use webbrowser. navigate (URL) to open a new window without calling IE.

But sometimes, statustext is not an accurate URL for the new window. At this time, webbrowser will open the wrong webpage. Don't believe it. If you know programming, you can try the Baidu button on www.hao123.com. What you get is the URL of www.baidu.com/s, but this is not a real URL...

In fact, the webpage opens a link in a new window because the link has a property target set to blank. If we set it back to _ self, it will be OK...

Based on the above analysis, my solution is as follows:

CodeLet's explain 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
{
}
}

This is the first avoidance. The second analysis implementation is as follows. The target attribute of all the controls on the loaded webpage is changed to _ 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 web page is finally loaded, call the above method as follows:

// Before complete, set the target of all controls on the document to _ Self. To avoid this, the new window is changed to IE by default.
This. setallwebitemself (this. wb_mainbrowser.document.all );

For more information about how to determine the final Loading Method of webbrowser, see another article.Article[Original] C # webbrowser accurately determines the final loading of webpages

In this way, once the webpage is loaded, the target of all the controls will be changed to _ Self. In the future, all the links you have clicked will be opened on this webpage, generally, mainbrowser_newwindow is not triggered. The first step is just in case.

In the above method, you can use the 90% link to open it on this webpage. Some of them are flash, image links or something, which are special and have some shortcomings.

You can try the search results of www.hao123.com Google if you don't believe it. It was originally played in the new ie window and is now opened in webbrowser.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.