0

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

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

4

1 回答 1

2

你应该使用EvaluateExpressionAsync.

_page.EvaluateExpressionAsync("JSON.stringify(performance.timeOrigin)").ConfigureAwait(false);
于 2020-03-14T22:14:16.467 回答