0

I'm a started in coding, and I try to autofill username and password into a page but when I click login, code still search for getelementbyid on the next page(all pages are under same domain) and crashes. I'm using Visual Studio 2k17

Thanks in advance, any help would be perfect!

using System;
using System.Windows.Forms;
using DotNetBrowser;
using DotNetBrowser.DOM;
using DotNetBrowser.Events;
using DotNetBrowser.WinForms;

namespace WindowsFormsApp9
{
    class Program
    {
        public class WindowMain : Form
        {
            private WinFormsBrowserView browserView;

            public WindowMain()
            {
                Browser browser = BrowserFactory.Create();
                browserView = new WinFormsBrowserView(browser);

                browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
                {

                    if (e.IsMainFrame)
                    {
                        DOMDocument document = e.Browser.GetDocument();
                        DOMInputElement username = (DOMInputElement)document.GetElementById("id_Username");
                        DOMInputElement password = (DOMInputElement)document.GetElementById("id_Password");


                        username.Value = "fo2";
                        password.Value = "f2342156f";

                    }
                };

                this.Controls.Add(browserView);

                Width = 1024;
                Height = 768;
                this.Load += WindowMain_Loaded;
            }

            void WindowMain_Loaded(object sender, EventArgs e)
            {
                browserView.Browser.LoadURL("http://test.com");
            }

            [STAThread]
            public static void Main()
            {
                WindowMain wnd = new WindowMain();
                Application.Run(wnd);

                var browser = wnd.browserView.Browser;
                wnd.browserView.Dispose();
                browser.Dispose();
            }
        }
    }
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125

1 Answers1

0

The Browser.FinishLoadingFrameEvent event is raised every time when a web page is fully loaded. So, the application is crashed, because it still setting values to elements which do not exist on the next web page.

To resolve this behavior, it is necessary to add the check with the validated URL and unsubscribe from the FinishLoadingFrameEvent after the login code executes.

Here is a reworked sample:

using System;
using System.Windows.Forms;
using DotNetBrowser;
using DotNetBrowser.DOM;
using DotNetBrowser.Events;
using DotNetBrowser.WinForms;

namespace WindowsFormsApp9
{
    class Program
    {
        public class WindowMain : Form
        {
            private WinFormsBrowserView browserView;

            public WindowMain()
            {
                Browser browser = BrowserFactory.Create();
                browserView = new WinFormsBrowserView(browser);

                browser.FinishLoadingFrameEvent += Browser_FinishLoadingFrameEvent;

                this.Controls.Add(browserView);

                Width = 1024;
                Height = 768;
                this.Load += WindowMain_Loaded;
            }

            private void Browser_FinishLoadingFrameEvent(object sender, FinishLoadingEventArgs e)
            {
                if(e.IsMainFrame && e.ValidatedURL.Contains("loginURL"))
                {
                    DOMDocument document = e.Browser.GetDocument();
                    DOMInputElement username = (DOMInputElement)document.GetElementById("id_Username");
                    DOMInputElement password = (DOMInputElement)document.GetElementById("id_Password");

                    username.Value = "fo2";
                    password.Value = "f2342156f";

                    e.Browser.FinishLoadingFrameEvent -= Browser_FinishLoadingFrameEvent;
                }
            }

            void WindowMain_Loaded(object sender, EventArgs e)
            {
                browserView.Browser.LoadURL("http://test.com");
            }

            [STAThread]
            public static void Main()
            {
                WindowMain wnd = new WindowMain();
                Application.Run(wnd);

                var browser = wnd.browserView.Browser;
                wnd.browserView.Dispose();
                browser.Dispose();
            }
        }
    }
}
Vladyslav Tsvek
  • 244
  • 1
  • 10