js.executeScript produces a Unexpected identifier error

Visit this link for proofs

Running a script in the browser console on the specified page:

let img = document.querySelector('.subscribe'),
style = img.currentStyle || window.getComputedStyle(img, false),
bi = style.backgroundImage.slice(4, -1).replace(/"/g, "");

Type 'bi' in the console and press enter. Expecting URL to be: "https://www.companyfolders.com/images/skin/bgr_header.png"

However, when attempting to execute this in Java: after navigating to the page, trying to run:

JavascriptExecutor js = (JavascriptExecutor) driver;
String a = String.valueOf(js.executeScript("return let img = document.querySelector('.subscribe'), style = img.currentStyle || window.getComputedStyle(img, false), bi = style.backgroundImage.slice(4, -1).replace(/\"/g, \"\");"));
System.out.println(a)

An error is encountered: org.openqa.selenium.JavascriptException: javascript error: Unexpected identifier. Trying to add a 'return' statement before 'bi' results in a JS return statement error.

Please provide guidance on implementing this script, running it, and returning the execution result (URL as a string) from Java Selenium?

Answer №1

When it's limited to just one statement...

return (document.querySelector('.subscribe').currentStyle || window.getComputedStyle(document.querySelector('.subscribe'), false)).backgroundImage.slice(4, -1).replace(/\"/g, \"\");")

This solution is effective! Gratitude to Jaromanda X

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

Checking the availability of a username by sending an Ajax request every time the user types it may lead to inefficiencies and slower response times. Are there

I have developed a NodeJS authentication application. In this scenario, when a user enters a username, the system will display "username taken" if it is already in use, otherwise it will show "username available". I am interested in understanding the lim ...

Is it possible to choose multiple buttons with identical IDs using Python Selenium?

I am dealing with two sets of buttons that have the same id and class name. <div id="bedroomNum-input" class="pageComponent" data-label="CONFIG_BEDROOM"> <span class="labels_semiBold radioInput_bubbleLable__1 ...

Ways of extracting specific information from a JSON file with the help of jQuery

I am currently attempting to parse a JSON file that is stored locally on my system using jQuery. I am specifically interested in retrieving certain data from the file, which is structured like this: {"statements":[{"subject":{"uriString":"A","localNameIdx ...

"Encountering an error with ExpressJS where it cannot GET a file, preventing access to other folders' content

My goal is to set up a simple server using expressJS to retrieve data for my Angular application. However, I've encountered an error that says 'Cannot GET/'. This is the code snippet of the webserver I attempted to create: var express = re ...

Can a robust web application be developed using Kotlin in Node.js?

With the recent release of Kotlin 1.1, it is now possible to compile Kotlin projects into JavaScript as a target, allowing for full JavaScript compilation. Can a Node.js application, like an express webserver, be developed entirely using Kotlin code? As s ...

What is the reason for the failure of multiple place markers on a planet object3D?

After spending hours trying to debug a piece of code I wrote 3 years ago using Three.js, I still can't figure out why it's not working anymore. I thought updating all the other code to use ES6 for Three.js would solve the issue, but when I try t ...

Angular, a Self-sufficient Module of Cascading Style Sheets

I have developed a factory that generates HTML content. I want to showcase this generated HTML within a specific section of my application, without its CSS affecting the rest of the site, and vice versa. While using an iframe is one option, I believe there ...

Is there a way to efficiently navigate a local JSON file using React JS?

What is the best way to extract data from a JSON file and utilize it within my code? I attempted importing the file and logging it in the console, but all I get is Object {}: import jsonData from "./file.json"; console.log(jsonData); This is the content ...

How can you efficiently update another ng-model value based on a select input in AngularJS using ng-change?

<select data-ng-init="selectedItem='previewWidth = 1920; previewHeight = 1080'" data-ng-model="selectedItem" data-ng-change="GetNewData(); {{selectedItem}}"> <option value="previewWidth = 1920; previe ...

What steps do I need to follow in order to incorporate and utilize an npm package within my Astro component

I have been working on integrating KeenSlider into my project by installing it from npm. However, I am encountering an error message that says Uncaught ReferenceError: KeenSlider is not defined whenever I try to use the package in my Astro component. Belo ...

What could be causing the error I'm encountering while running the 'net' module in Node.js?

I am currently working with .net modular and have opened TCP port 6112. var net = require('net'); var server = net.createServer(function (socket) { //'connection' listener }); server.listen(6112, function () { //'listening ...

Symbol not located within the comparable array

I am working on creating a stack of comparable objects based on a specific interface. Within my class, here is the constructor I have implemented: public MyStack() { Comparable[] array = new Comparable[INITIAL_SIZE]; size = 0; } However, whenever I ...

Troubleshooting error messages in the console during conversion of image URL to Base64 in AngularJS

While attempting to convert an image URL to Base64 using the FromImageUrl method, I encountered an error in my console. Access to the image located at '' from the origin 'http://localhost:8383' has been blocked due to CORS policy ...

Updating selenium element using Java language

Currently, I am in the process of creating an automated bot that is able to extract data from a specific website. While the bot successfully reads the data as expected, I encountered the need to implement intervals using Timers. To achieve this, I created ...

Issue with smart table sorting functionality

I've been working on a table with just one column that is populated from a string array. However, I'm struggling to get the sorting functionality to work properly. Can anyone pinpoint what mistake I might be making here? Steps taken so far: 1) ...

Can the geocoder API/search box be utilized to locate specific markers on a map?

Incorporating Mapbox into an Angular application with a high volume of markers on the map (potentially thousands) and hoping to implement a search box for users to easily locate specific markers based on unique names and coordinates. Is this functionalit ...

The disappearance of the "Event" Twitter Widget in the HTML inspector occurs when customized styles are applied

Currently, I am customizing the default Twitter widget that can be embedded on a website. While successfully injecting styles and making it work perfectly, I recently discovered that after injecting my styles, clicking on a Tweet no longer opens it in a ne ...

Timer Tool: JavaScript Countdown Application

I am currently working on a JavaScript countdown timer, but I need help with setting it to only run for 48 hours every time the page is submitted. Right now, when I enter a time, the timer counts down to that specified time. Can someone please assist me ...

There seems to be a glitch preventing the Redis client from properly executing

Having some trouble with my Redis implementation in Node.js. Despite using async/await as recommended in the docs, I'm only seeing 'console log 1' being logged. Any ideas on what might be causing this issue? Any help or suggestions would be ...

The cleanSession setting for Paho MQTT was turned off, but messages are still not being received

While working on a project, I decided to test out MQTT. I successfully managed to receive messages on a subscribed topic when the client is connected. The QoS is set to 1 and cleanSession is set to false. However, I encountered an issue where I cannot rece ...