引用
CefSharp的SendMouseClickEvent
,通过此函数发送鼠标点击事件。
操作方法
先借助jQuery获取指定DOM的偏移,然后发送SendMouseClickEvent
完成模拟点击操作。
函数
/// <summary>
/// 模拟点击指定选择符DOM元素
/// </summary>
/// <param name="selector">jQuery选择符</param>
private void ClickElement(string selector)
{
var sSel = $"$('{selector}').offset();";
Task<CefSharp.JavascriptResponse> t = Browser.EvaluateScriptAsync(sSel);
t.Wait();
if (t.Result.Result != null)
{
var expandoDic = t.Result.Result as IDictionary<string, object>;
if ((null != expandoDic) && expandoDic.ContainsKey("left") && expandoDic.ContainsKey("top"))
{
var left = Convert.ToInt32(expandoDic["left"]) + 5;
var top = Convert.ToInt32(expandoDic["top"]) + 5;
Browser.GetBrowserHost().SendMouseClickEvent(left, top, MouseButtonType.Left, false, 1, CefEventFlags.None);
Browser.GetBrowserHost().SendMouseClickEvent(left, top, MouseButtonType.Left, true, 1, CefEventFlags.None);
}
}