编程语言
首页 > 编程语言> > 如何在C#.net中使用Awesomium登录到Google帐户?

如何在C#.net中使用Awesomium登录到Google帐户?

作者:互联网

我正在学习Awesomium,下面是我尝试登录到https://accounts.google.com的代码.我能够成功设置页面中的登录名和密码字段值,但无法提交登录表单,单击有效.谁能帮我登录吗?

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

结果图片:

解决方法:

它正在工作,您只是过早获取屏幕截图.如果使用.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();
    }
}

标签:google-login,awesomium,c
来源: https://codeday.me/bug/20191028/1955070.html