How to login to google accounts using awesomium in c # .net?

I am learning Awesomium and the following code where I am trying to log into https://accounts.google.com . I can successfully set the login and password field values ​​on the page, but was unable to submit the login form and this click does not work. Can anyone help me log in?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Awesomium.Core;


namespace Awesom
{
    class Program1
    {
        public static void Main(String[] args)
        {
            Console.WriteLine("Started....");

            WebView wv = WebCore.CreateWebView(1024, 600);
            wv.Source = new Uri("https://accounts.google.com");
            wv.LoadingFrameComplete += (s, e) =>
            {
                if (!e.IsMainFrame)
                    return;

                dynamic document = (JSObject) wv.ExecuteJavascriptWithResult("document");

                using(document)
                {
                    //Works
                    var tbox = document.getElementById("Email");
                    tbox.value = "XXXXXXXX@gmail.com";

                    //Works
                    var pbox = document.getElementById("Passwd");
                    pbox.value = "**********";

                    //Doesnt work
                    var lform = document.getElementById("gaia_loginform");
                    lform.submit();

                    //Doesnt work
                    var sbox = document.getElementById("signIn");
                    sbox.click();
                }

                BitmapSurface surface = (BitmapSurface)wv.Surface;
                surface.SaveToPNG("result.png", true);

                WebCore.Shutdown();
            };

            WebCore.Run();
        }
    }
}

      

Results Image:

enter image description here

+3


source to share


1 answer


It works, you just take a screenshot too early. You need to consider second frame navigation if you are using .click()

.



public static void Main(String[] args)
{
    Console.WriteLine("Started....");

    WebView wv = WebCore.CreateWebView(1024, 600);

    wv.Source = new Uri("https://accounts.google.com/");

    FrameEventHandler handler = null;
    handler = (s, e) =>
    {
        if (e.IsMainFrame)
        {
            // we have finished loading main page,
            // let unhook ourselves
            wv.LoadingFrameComplete -= handler;

            LoginAndTakeScreenShot(wv);
        }
    };

    wv.LoadingFrameComplete += handler;

    WebCore.Run();
}

private static void LoginAndTakeScreenShot(WebView wv)
{
    dynamic document = (JSObject)wv.ExecuteJavascriptWithResult("document");

    using (document)
    {
        //Works
        var tbox = document.getElementById("Email");
        tbox.value = "XXXXXXXX@gmail.com";

        //Works
        var pbox = document.getElementById("Passwd");
        pbox.value = "**********";

        FrameEventHandler handler = null;
        handler = (sender, args) =>
        {
            if (args.IsMainFrame)
            {
                wv.LoadingFrameComplete -= handler;

                BitmapSurface surface = (BitmapSurface)wv.Surface;
                surface.SaveToPNG("result.png", true);

                WebCore.Shutdown();
            }
        };

        wv.LoadingFrameComplete += handler;

        var sbox = document.getElementById("signIn");
        sbox.click();
    }
}

      

+4


source







All Articles