如何使用PuppeteerSharp获取performance.timeOrigin的值?

How to use PuppeteerSharp to get the value of performance.timeOrigin?

使用 PuppeteerSharp,我尝试使用下面的 C# 代码获取 performance.timeOrigin 值:

JToken performanceTimeOriginString = await _page.EvaluateFunctionAsync("JSON.stringify(performance.timeOrigin);").ConfigureAwait(false);

我已经验证这在 JavaScript 控制台中有效:

但是,当我 运行 上述 PuppeteerSharp 代码时,出现以下异常:

System.AggregateException: One or more errors occurred. (Evaluation failed: SyntaxError: Unexpected token ';') ---> PuppeteerSharp.EvaluationFailedException: Evaluation failed: SyntaxError: Unexpected token ';'
at PuppeteerSharp.ExecutionContext.ExecuteEvaluationAsync(String method, Object args)
at PuppeteerSharp.ExecutionContext.RemoteObjectTaskToObject[T](Task`1 remote)
at PuppeteerSharp.DOMWorld.EvaluateFunctionAsync[T](String script, Object[] args)
--- End of inner exception stack trace ---

它告诉我删除分号(非常奇怪),所以我尝试了但随后出现以下异常。

System.AggregateException: One or more errors occurred. (Protocol error (Runtime.callFunctionOn): Given expression does not evaluate to a function) ---> PuppeteerSharp.EvaluationFailedException: Protocol error (Runtime.callFunctionOn): Given expression does not evaluate to a function ---> PuppeteerSharp.MessageException: Protocol error (Runtime.callFunctionOn): Given expression does not evaluate to a function
at PuppeteerSharp.CDPSession.SendAsync(String method, Object args, Boolean waitForCallback)
at PuppeteerSharp.CDPSession.SendAsync[T](String method, Object args)
at PuppeteerSharp.ExecutionContext.ExecuteEvaluationAsync(String method, Object args)
--- End of inner exception stack trace ---

如何使用 PuppeteerSharp 获取 performance.timeOrigin 的值?

你应该使用 EvaluateExpressionAsync.

_page.EvaluateExpressionAsync("JSON.stringify(performance.timeOrigin)").ConfigureAwait(false);