使用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 ---如何使用performance.timeOrigin**?**获取的值?
发布于 2020-03-14 22:14:16
您应该使用EvaluateExpressionAsync。
_page.EvaluateExpressionAsync("JSON.stringify(performance.timeOrigin)").ConfigureAwait(false);https://stackoverflow.com/questions/60687524
复制相似问题