Tips for obtaining the JSessionID while using PhantomJS

I'm having trouble determining if this issue is related to PhantomJS or just plain Javascript.

My current setup involves using Ghostdriver to open a webpage and attempt to retrieve the response headers. GhostDriver runs the javascript and uses the onResourceReceived event for this purpose.

This is my code snippet:

String responsescript = 
                "var page = this,"+
                "jsonResponse = \"\";"+
                "page.onResourceReceived = function (res) {"+
                "console.log(JSON.stringify(res));" /* The logging part works, but I need to somehow pass this data back to my Java program. I attempted the following alternative approach, which unfortunately did not work */
                "jsonResponse = jsonResponse + JSON.stringify(res, undefined, 4);"+
                "};"+
                "function getJsonResponse(){"+
                "return jsonResponse;"+
                "}";
ghostDriver.executePhantomJS(responsescript);
ghostDriver.get("cnn.com");
ghostDriver.executePhantomJS("getJsonResponse();");

However, I keep encountering the same error message:

{message=Can't find variable: getJsonResponse, line=1, stack=ReferenceError: Can't find variable: getJsonResponse

All I want to do is store the response headers in a String variable in Java so that I can search for JSESSIONID within it.

Due to my limited knowledge of Javascript, I am struggling to resolve this seemingly simple problem.

Answer №1

It’s funny how I ended up taking the longer route when all it took was to look at the examples on ghostdriver(link) to see that it's just 2 lines of code.

Feeling so relieved now...time for a peaceful sleep :)

    String extractscript = 
                    "return JSON.stringify(this.cookies);";
    Object result = phantom.executePhantomJS(extractscript);

    System.out.println(((String)result));

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

blending javascript and php variables within ajax requests

I am working on a simple jQuery ajax feature, where I need to combine form data retrieved by JS with some PHP variables and send them all through ajax GET method. Here's what I have: var longform = $("input:text").serialize(); $.ajax({ url: & ...

Customizing a Color in Vuetify by Adjusting the Hex Code

Are you looking to customize the hexcodes for the colors listed on this Vuetify page? For example, if you wanted to adjust the red color to be a bit lighter, how would you go about doing that? According to the documentation, you may have something like t ...

`I'm encountering issues when trying to pass an array through localStorage into a new array`

This is a complex and detailed question that I am struggling to find a solution for. Despite using deprecated mysql due to hosting limitations, the problem lies elsewhere. Part 1 involves dataLoader.php, which queries the database and retrieves posx and p ...

Issue encountered in undetectable chromedriver while utilizing a proxy: ERR_TUNNEL_CONNECTION_FAILED

When configuring my proxy in the following manner: options = uc.ChromeOptions() options.add_argument(f'--proxy-server={host}:{port}') I am encountering an ERR_TUNNEL_CONNECTION_FAILED error on numerous websites. Upon attempting to implement the ...

Tips for saving PDF files in html-pdf-node

I am running into an issue where I am trying to save a PDF file in the current directory, but it's not working. It seems to be creating a buffer, but the file is not being saved. var pdf_converter = require('html-pdf-node'); let settings ...

When there is no data in the request body, ExpressJS will display it as empty

A big part of my tech stack includes a website with an express server up and running. The website allows users to insert their username and password. Upon clicking the login button, the server receives a request with the username and password packed as a J ...

The expected functionality of sending files via ajax is not happening as anticipated

I am having issues with passing file data along with other inputs to my ajax function. Despite my best efforts, the server is not receiving the files. I'm fairly new to using ajax and Jquery. Below is the code snippet of what I have attempted so far. ...

Steps for configuring a Handler/Looper to invoke requestLocationUpdates within a Service callback

In my current project, I have a service that creates a web server using com.koushikdutta.async.AsyncHttpServer. Within a callback function, I need to instantiate a class that calls requestLocationUpdates on LocationManager, but this action is triggering an ...

Send a parameter to a React hook

I am facing an issue with a Component that is using a separate hook. I am unable to pass any value to that hook, which is necessary for my use case. const onEdit = (index: number) => useMediaLinkImage(img => { setNodeImages(imgData => { ...

What is the alternative way to achieve this functionality in Javascript without relying on jQuery?

I am encountering some dependencies issues with jQuery that are preventing me from using it. However, I still need to find a way to make this work without relying on jQuery libraries. Do you have any suggestions on how to achieve this? $(document).ready ...

Switching the directional arrow images using jQuery

How can I change my image to a down arrow using jQuery? I'm trying to make the up arrow switch to a down arrow when a user opens a drop-down menu. I've attempted to do this using an if-else statement, but it doesn't seem to be working. The a ...

Retrieving package information from NPM using a web browser

I am currently developing a Chrome Web App that retrieves information from NPM. However, I have encountered issues due to Chrome adhering to the Access-Control-Allow-Origin flags set by websites. While I am able to access the following URL: http://regist ...

Can anyone help me troubleshoot this issue with uploading external JS scripts into my HTML code?

I'm currently facing an issue with my HTML document where the external js file is not loading. Here's a snippet of the HTML: <!DOCTYPE html> <html> <head> <title>...</title> <meta name="viewport" conten ...

Is it possible to add an element to the beginning of an array using a chained method without relying on the map function?

Looking for a solution to add an element at the start of an array in a method chain led me to some interesting findings. Initially, I attempted: console.log( [1, 2, 3] .map(a=>a+1) .splice(0, 0, 7) .map(a=>a*10) ) Usin ...

Retrieve specific attribute values from JSON in Java without having to map the entire object

As a newcomer to the world of JSON, I have been scouring various answers on how to convert JSON to JAVA Object. But here's my dilemma. I have this massive JSON data and all I need is to extract specific information from it. The data includes objects ...

What is the best way to fill a list-group using JavaScript?

Fetching ticket data from a controller is done using the code snippet below: $.ajax({ type: "GET", url: "/Tickets/List/", data: param = "", contentType: "application/ ...

Emulate the HTML form process on an Android application

Hi there, I am seeking assistance in implementing the HTML code below into my Android application. **** HTML **** Test it out here: on GitHub: https://github.com/babaphemy/buycards The form: <?php session_start(); $amt1 = $_POST['amount'] * ...

When we typically scroll down the page, the next section should automatically bring us back to the top of the page

When we scroll down the page, the next section should automatically bring us back to the top of the page without having to use the mouse wheel. .bg1 { background-color: #C5876F; height: 1000px; } .bg2 { background-color: #7882BB; height: 1000px; } .bg3 ...

Interacting between tabs in JavaFx and Scene Builder

In my project, I created a MainEntredController that includes a TabPane with two tabs: HomeController and PatientController. The main issue I am facing is that the tabs are not recognizing the parent (MainEntredController) or each other. An error occurs in ...

What could be causing my directive to not display my scope?

I am currently developing a directive that includes a custom controller, and I am testing the scope functionality. However, I am facing an issue where the ng-show directive is not functioning as expected when trying to test if the directive has a scope fro ...