How to simulate an "open link in a new window" in C # WebBrowser?

I just found out that to simulate a click, I can call element.InvokeMember("click");where element- HtmlElement. But I really need to open the link in a new window, but not in the default browser, but in another WebBrowser, which I will create in my program. Sometimes it works to just get the href attribute by calling element.GetAttribute("href");and then just going to the returned url, but some picky web pages will not work that way, I assume you are making cookies and sessions.

+3
source share
2 answers

Just handle the NewWindow2 event, create a form / tab on it with a web browser and use the web browser as the target of the new window request. For example, consider http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx .

0
source

System.Windows.Forms.WebBrowser- a very crippled control and one of the biggest problems is multi-tabbing support. This does not support him at all.

I spent a lot of time trying to get it to work correctly, but did not get enough success, so we recommend that you try third-party management.

: click <a> ( ) . , , dotBrowser: 1 2

foreach (HtmlElement tag in webBrowser.Document.All)
{
    tag.Id = String.Empty;
    switch (tag.TagName.ToUpper())
    {
        case "A":
        {
            tag.MouseUp += new HtmlElementEventHandler(link_MouseUp);
            break;
        }
    }
}

private void link_MouseUp(object sender, HtmlElementEventArgs e)
{
    mshtml.HTMLAnchorElementClass a = (mshtml.HTMLAnchorElementClass)((HtmlElement)sender).DomElement;
    switch (e.MouseButtonsPressed)
    {
        case MouseButtons.Left:
        {
            // open new tab
            break;
        }
        case MouseButtons.Right:
        {
            // open context menu
            break;
        }
    }
}
+1

Source: https://habr.com/ru/post/1758339/


All Articles