0

I am using .NET 4.8 WebBrowser component, while using web page that creates new window on submit, it creates new Internet Explorer process with a Tab of this new window.

This is a problem for me as what I need, is the content of this new window.

I tried using NewWindow event on WebBrowser, to e.Cancel this event and redirect WebBrowser component to desired NewWindow URL, but it does not work, as this webSite uses some form of App PostBack that returns only one time resutls.

Then I tried to use

AutoItX.WinGetText("Window Title"); //Of course I changed this to real window title

But it does not show one thing and that is content of the TabPage This is how WebBrowser component creates NewWindow which I do not have access to, Google Search is just example

Is there any way to read content of Internet Explorer tab please ?

I have tried:

AutoItX.WinGetText("Window Title"); //Of course I changed this to real window title

and also

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            webBrowser1.Navigate("url");
        }

WebPage: cica.vugk.sk , Navigate to 'vlastnik spravca', fill 'prvé písmeno priezviska' to A, click 'Vytvor LV', it creates popup that I need content of and I did not find any solution how to access it using WebBrowser, or WebView2.

  • 1
    _"I am using .NET 4.8 WebBrowser component"_ - **why** are you using `WebBrowser` in 2022? – Dai Nov 06 '22 at 11:59
  • Handle `Navigating`? – Anders Nov 06 '22 at 12:44
  • I am using WebBrowser because I not very experienced and I want to use methods like GetElementById, SetAttribute, RaiseEvent – Kristián Moser Nov 06 '22 at 14:18
  • @KristiánMoser `WebBrowser` uses IE11's `MSHTML.dll` which means it can't render most modern websites on the Internet today (i.e. you're ruining your UX). [You **need** to use `WebView2` instead](https://learn.microsoft.com/en-us/microsoft-edge/webview2/get-started/winforms). I do sympathise with your concerns about the removal of direct DOM access, but you can still accomplish almost everything with the `ExecuteScriptAsync` method - there are also libraries that add DOM access back: e.g. https://github.com/R2D221/WebView2.DOM [and others](https://stackoverflow.com/q/62328943/159145) – Dai Nov 06 '22 at 14:27
  • @Dai Thank you Dai, I already started upgrading to WebView2 as for your recommendation. For me the UX and rendering is not issue now. I am not sure if WebView2 will solve my problem, as it is, that using WebPage: cica.vugk.sk , Navigate to 'vlastnik spravca', fill 'prvé písmeno priezviska' to A, click 'Vytvor LV', it creates popup that I need content of and I did not find any solution how to access it using WebBrowser, or WebView2. – Kristián Moser Nov 06 '22 at 14:38
  • If you’re just after some HTML from a webpage, why not use AngleSharp instead? Or do you need JS support and/or “real” browser behaviour? – Dai Nov 06 '22 at 17:44

1 Answers1

0

C# WebBrowser is forced to open in this window, and prohibited to open in a new window.

Use the load complete event to change the target value of all links and forms to "_self":

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    foreach (HtmlElement archor in this.webBrowser.Document.Links)
    {
        archor.SetAttribute("target", "_self");
    }
    foreach (HtmlElement form in this.webBrowser.Document.Forms)
    {
        form.SetAttribute("target", "_self");
    }
} 

Cancel the new window event:

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
     e.Cancel = true;
} 

WebBrowser property settings:

  • Set AllowWebBrowserDrop of WebBrowser to false (no drag and drop)
  • Set WebBrowserShortcutsEnabled of WebBrowser to false (disable shortcut keys)
  • Set WebBrowser's IsWebBrowserContextMenuEnabled to false (disable right-click context menu)
桑榆肖物
  • 73
  • 2
  • 8
  • Or you can try cefsharp or webview2 , they are easier to use. – 桑榆肖物 Nov 06 '22 at 12:54
  • Thank you, I used your code, but then, nothing happens. WebPage is: https://cica.vugk.sk/ , Navigate to 'vlastnik spravca', fill 'prvé písmeno priezviska' to A, click 'Vytvor LV', it creates popup, with your code and all of this in WebBrowser component, nothing happens, but many thanks for the post. – Kristián Moser Nov 06 '22 at 14:11
  • I tested this webpage, I edited the `target` attribute of the form of this html page in edge to `_self`, but it became _blank after submitting, so amazing – 桑榆肖物 Nov 06 '22 at 14:44
  • Submit button inspect contains: I think that '_blank' needs to be replaced somehow to not show blank page but I do not know the answer yet, going to try – Kristián Moser Nov 06 '22 at 16:53