如果需要评估返回值的代码,使用 Task<JavascriptResponse> EvaluateScriptAsync(string script, TimeSpan? timeout)
方法。 JavaScript代码是异步执行的,因此使用.NET Task
类返回一个响应,其中包含错误消息,结果和一个成功(bool)标志。
// Get Document Height
var task = frame.EvaluateScriptAsync("(function() { var body = document.body, html = document.documentElement; return Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight ); })();", null);
task.ContinueWith(t =>
if (!t.IsFaulted)
var response = t.Result;
EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
}, TaskScheduler.FromCurrentSynchronizationContext());