CefSharp向瀏覽器中指定DOM傳送點選事件
阿新 • • 發佈:2020-11-14
引用
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);
}
}