首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PuppeteerSharp获取performance.timeOrigin的值?

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

Stack Overflow用户
提问于 2020-03-14 21:35:52
回答 1查看 1.3K关注 0票数 0

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

代码语言:javascript
复制
JToken performanceTimeOriginString = await _page.EvaluateFunctionAsync("JSON.stringify(performance.timeOrigin);").ConfigureAwait(false);

我已经验证了这在JavaScript控制台中是可行的:

但是,当我运行上述PuppeteerSharp代码时,会得到以下异常:

代码语言:javascript
复制
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 ---

它告诉我删除分号(非常奇怪),所以我尝试了,但后来得到了下面的例外。

代码语言:javascript
复制
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**?**获取的值?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-14 22:14:16

您应该使用EvaluateExpressionAsync

代码语言:javascript
复制
_page.EvaluateExpressionAsync("JSON.stringify(performance.timeOrigin)").ConfigureAwait(false);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60687524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档