Obtaining a JSONArray using JavascriptExecutor

Is there a way to extract a JSONArray from JavascriptExecutor in Selenium? Normally, when I try to access it using "___grecaptcha_cfg.clients[0]" in Chrome's dev console, I get a result similar to this:

https://i.sstatic.net/X8BlS.png

However, when I attempt the following code snippet:

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

I encounter an error:

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 suggestions on how to resolve this issue?

Thank you

UPDATE

If I use the following code instead:

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

I receive the desired output:

{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}

The issue lies with the changing value of Cy.C. How can I handle this dynamically changing value?

Update 2

By using:

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

I encounter the error message

unknown error: Converting circular structure to JSON

It seems like I may be running into infinite recursion. Any advice on how to solve this problem? My goal is to parse out the value 'Cy.C' by identifying callback={} or sitekey={} within the object.

Answer №1

To handle a complex return JSON object, one approach is to convert it into a string and then parse it back within the JAVA code.

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

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

Answer №2

The issue with the error message "unknown error: Maximum call stack size exceeded" was due to the complexity of the object I was searching for.

After noticing several unanswered posts about similar issues, I decided to share my solution:

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);

In this code snippet, I am only retrieving the first property from the object. It became apparent that unless I iterated through the entire object, I wouldn't get the correct order.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The issue lies with the Cookies.get function, as the Typescript narrowing feature does not

Struggling with types in TypeScript while trying to parse a cookie item using js-cookie: // the item 'number' contains a javascript number (ex:5) let n:number if(typeof Cookies.get('number')!== 'undefined'){ n = JSON.pars ...

Can you explain the distinction between using a mouse to click on an input versus selecting it with the tab key?

I have a unique input field that I'm working with: <div class="btn-group btn-xs" dropdown> <input id="simple-btn-keyboard-nav" ng-model="available_fields_query" id="single-button" dropdown-toggle ng-disabled="disabled" placeholder="Add New ...

What is the method for retrieving the index of an array from an HTML element?

Howdy! Within my Vue application, I have the ability to create multiple individuals: <div v-for="newContent in temp" :key="newContent.id" @click="showId(newContent.id)" v-show="true"> <!-- ...

What's Next? Redirecting Pages in Node.js Express after Handling POST Requests

Is it possible to redirect to a different page from a post request? module.exports = function(app) { app.post('/createStation', function(request, response){ response.redirect('/'); //I'm having trouble getting ...

Java: Surprising outcomes when combining a String with an expression using the plus + operator

Consider the code snippet below: public class Test { public static void main(String... strings) { System.out.println("String, " + false); System.out.println("String, " + getFalse()); System.out.println("String, " + new TestClass()); ...

Check for available usernames in real-time using Node.js and Express.js, along with client-side JavaScript

Currently, I am working on a web application using Express, MongoDB, and Handlebars as the templating engine. In this app, there is a form where users can create their unique usernames. I want to implement a feature where a tooltip pops up at intervals to ...

Using JavaScript, capture the current date and time and store it in a .txt file with the corresponding format

Objective: Upon clicking the download link, an automatically named file will be downloaded with today's date and time format. For example, 7-3-2021 04:04:00 PM or any applicable format with the date and time included in the name. Below is the code sn ...

Is there a way to transfer information from the Chrome console to a Python list?

Currently, I am utilizing Python along with Selenium and Chrome Driver for my project. My goal is to extract data from the Chrome console and store it in a list using Python. As shown in the image provided, I am specifically interested in extracting all th ...

Arranging a 2D array in Java

Is there a distinction between the following two code snippets for sorting arrays? Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1])); Arrays.sort(points, (a, b) ->{ return a[1] - b[1]; }); ...

Can you please guide me on determining the type of the root element using jQuery?

I have an ajax request that can return either an error code or a user ID <?xml version="1.0" encoding="UTF-8"?> <error>1000</error> <?xml version="1.0" encoding="UTF-8"?> <userID>8</userID> Is there a way to determine ...

Disable the ng2-tooltip-directive tooltip when the mouse is moved

Is there a way to hide the tooltip when the mouse enters? How can I achieve this? <div (mousemove)="closeTooltip()" [tooltip]="TooltipComponent" content-type="template" show-delay="500" placement= ...

The previous and next arrows do not have a looping feature

I have a collection of 10 HTML pages stored in a folder called PROJECTS. Everything is functioning properly, except for the prev and next arrow navigation buttons. The issue arises when navigating to the last page (e.g., projectPage_10.html) from my Projec ...

Troubleshooting problem with connecting AngularJS ng-click within a directive and ControllerAs

ShowPopover is not triggering when the directive is clicked. Can you please assist me in diagnosing the source of this issue? Directive: angular.module('landingBuilder').directive('popoverDirective', popoverDirective); functi ...

Selenium: Exploring Dropdown Menus

The dropdown menu is not populating the 'from' and 'to' fields. Here is the code snippet causing the issue: public class MyFirst { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\&b ...

Angular monitoring problem

Today marks my first attempt at utilizing Angular's watch function, but I'm having trouble getting it to function properly. Within my codebase, there exists a service named apiService, housing a variable called myFile. By injecting this service i ...

What steps can be taken to resolve the error ERROR TypeError: undefined is not an object when evaluating 'userData.username'?

.I need help fixing this error ERROR TypeError: undefined is not an object (evaluating 'userData.username') Currently, I am working on a small application where users are required to allow permission for their location in order to save their cit ...

Utilizing attributes as scope properties within AngularJS

I am currently working on a directive and I need to pass the Attributes (Attrs) to the $scope, however, I am facing some difficulties in achieving this. Specifically, my goal is to assign attributes in my template based on the name set in my date-picker ta ...

Best method to generate an element using any jQuery selector string

What I want to accomplish I am looking to create an element that matches any given selector string. Here's a quick example: var targetString = "a.exaggerated#selector[data-myattr='data-here']"; var targetEl = $(targetString); if(!targetE ...

Using JavaScript to add a Vue component and display it on a page

I am working with a Vue component that is imported in a component file. My goal is to render another component using the append function. How can I achieve this? <template> <JqxGrid :width="width" :source="dataAdapter" :columns="gridValues" ...

I am looking to switch themes on the homepage using a button from an imported component

I have successfully created a toggle button on my main page in app.js to switch between dark and light themes. However, I am facing difficulty in putting the button inside my nav component and using it from that page imported in app.js. Can anyone guide me ...