selenium.common.exceptions.JavascriptException: Message: Cyclic object value

场景:

使用selenium3.x测页面性能,获取页面加载时间等信息

 

现象:

-> loadEntries = self.webdriver.execute_script("return window.performance.getEntries()")
(Pdb)
selenium.common.exceptions.JavascriptException: Message: Cyclic object value

 

原因:

JSON.stringify报cyclic object value错误,这是一个典型的循环引用的错误,一个对象里引用自己就会立刻得到这个错误:

1 obj = { x:555, y: "hi" };
2 obj.myself = obj;
3  
4 try{
5     json = JSON.stringify(obj);
6     alert(json)
7 }catch(e){
8     alert(e);
9 }
View Code

 

 

解决:

loadEntries = self.webdriver.execute_script("var result=[];window.performance.getEntries().forEach(function (perf){result.push({'responseEnd':perf.responseEnd})});return result")

posted @ 2020-09-22 14:07  Vincy_Wang  阅读(1034)  评论(0编辑  收藏  举报