如何在 C#.net 中使用 awesomium 登录 google 个帐户?
How to login to google accounts using awesomium in C#.net?
我正在学习 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();
}
}
我正在学习 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();
}
}