简体   繁体   English

从JavascriptExecutor返回JSONArray

[英]Returning a JSONArray from JavascriptExecutor

I am trying to return a JSONArray from JavascriptExecutor. 我正在尝试从JavascriptExecutor返回JSONArray。 Normally I can goto chrome browser and goto chrome's dev console and type: "___grecaptcha_cfg.clients[0]" which returns something similiar to this: 通常,我可以转到chrome浏览器和chrome的dev控制台,然后键入: "___grecaptcha_cfg.clients[0]" ,它返回与此类似的内容:

在此处输入图片说明

Though when I try this: 虽然当我尝试这个:

JavascriptExecutor js = (JavascriptExecutor) Browser;         
Object  o = (Object) js.executeScript("return ___grecaptcha_cfg.clients[0];");

I get: 我得到:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Maximum call stack size exceeded (Session info: chrome=69.0.3497.100) (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),
platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds

Any idea of how I can overcome this? 关于如何克服这一点的任何想法?

Thanks 谢谢

UPDATE UPDATE

It seems if I use: 看来如果我使用:

 Object o = (Object) js.executeScript("return ___grecaptcha_cfg.clients[0].Cy.C;");

I get back: 我回来了:

{action=null, badge=bottomright, bind=null, callback={}, content-binding=null, pool=null, preload=null, s=null, sitekey=flkgjsfldkjgsfdg, size=invisible, stoken=null, tabindex=null, theme=null, type=image}

which is what I am looking for, but the Cy.C is constantly changing. 这是我要寻找的,但是Cy.C一直在变化。 I need to be able to parse out the correct value that may change which currently is: 我需要能够解析出可能会改变的正确值,当前是:

Cy.C

I'm assuming the value is too long? 我假设值太长? How could I correct this issue? 我该如何解决这个问题?

Update 2 更新2

using: 使用:

String script = "return JSON.stringify(___grecaptcha_cfg.clients[0]);";
String str = (String) js.executeScript(script);

returns me unknown error: Converting circular structure to JSON 返回我unknown error: Converting circular structure to JSON

From searching around it seems my issue may be infinite recursion? 通过搜索,似乎我的问题可能是无限递归? Any suggestions on how to get around this? 关于如何解决这个问题的任何建议? I do not need the returned object to be in json format, I just need to be able to parse out whether through a regex expression or something else: 'Cy.C' which I can find by looking for callback={} or sitekey={} so I can build a string to call the callback. 我不需要返回的对象为json格式,我只需要能够通过正则表达式或其他内容解析出“ Cy.C”,就可以通过查找callback={}sitekey={}以便我可以构建一个字符串来调用回调。

If the return JSON object is complex, you can convert it to a string and return. 如果返回的JSON对象很复杂,则可以将其转换为字符串并返回。 Then convert the JSON string in JAVA code. 然后在JAVA代码中转换JSON字符串。

String script = "return JSON.stringify(___grecaptcha_cfg.clients[0].Cy.C);";
String str = (String) js.executeScript(script);

// Using JSON-Java lib to convert JSON string to JSON Java Object.
//
// 

The reason unknown error: Maximum call stack size exceeded was throwing was because the object I was searching for was way too complex. unknown error: Maximum call stack size exceeded原因unknown error: Maximum call stack size exceeded抛出unknown error: Maximum call stack size exceeded是因为我正在搜索的对象过于复杂。

since I saw alot of unanswered posts relating to similar things I will post what I did to solve it: 由于我看到了很多与类似事情有关的未回答的帖子,因此我将发布解决该问题的方法:

String script = "for (var prop in ___grecaptcha_cfg.clients[0])" 
               +"{"
               +" return '___grecaptcha_cfg.clients[0].' + prop"
               +"}";
System.out.println(script);
Object objects = (Object) js.executeScript(script);
System.out.printl(objects);

I am only pulling the first property here.. And from what I saw unless I iterated over the whole object I wouldn't get the correct order. 我只是在这里拉第一个属性。从我所看到的,除非我遍历整个对象,否则我将无法获得正确的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM