如何在 C#.net 中使用 awesomium 登录 google 帐户?

2024-04-21

我正在学习 Awesomium,以下是我尝试登录的代码https://accounts.google.com 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 = "[email protected] /cdn-cgi/l/email-protection";

                    //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 = "[email protected] /cdn-cgi/l/email-protection";

        //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();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C#.net 中使用 awesomium 登录 google 帐户? 的相关文章