How to login to google accounts using awesomium in

2019-05-18 03:24发布

I'm learning Awesomium and following is the code in which I'm trying to login to https://accounts.google.com. I'm successfully able to set the login and password field values in the page, but not able to submit the login form, neither does the click works. Can anyone help me how to login?

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();
        }
    }
}

Result image:

enter image description here

1条回答
甜甜的少女心
2楼-- · 2019-05-18 03:46

It IS working, you're just taking the screenshot too early. You need to account for the second frame navigation, if you use .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's 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();
    }
}
查看更多
登录 后发表回答