Use the JavaScript executor to combine a dynamic string

I have a String variable that retrieves IDs from an Excel sheet.

String id = formatter.formatCellValue(sheet.getRow(i).getCell(2));

I am trying to dynamically update the ID using JavaScript executor, but it seems that the concatenation is not working correctly:

je.executeScript("document.querySelector('.js-react-id-"+id+" button').click()");
System.out.println("Clicked on ID " + id);
je.executeScript("document.querySelector('div[data-id='" + id + "'] .sg-action span:last-of-type svg').click()");
System.out.println("Rated 5 stars for ID " + id);

Could you provide some guidance on what I might be doing wrong?

Answer №1

Make sure to properly escape the symbol ' with a backslash (\) like this

je.executeScript("document.querySelector('div[data-answer-id=\'" + answerID + "\'] .sg-rate-box__filled-stars span:last-of-type svg').click()");
System.out.println("Giving 5 stars to link " + link);

Answer №2

To start, include the following dependency in your pom:

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

Next, use the following code:

js.executeScript(StringEscapeUtils.unescapeEcmaScript("document.querySelector('.js-react-thanks-button-answer-id-"+answerID+" button').click()"));
js.executeScript(StringEscapeUtils.unescapeEcmaScript("document.querySelector('div[data-answer-id='" + answerID + "'] .sg-rate-box__filled-stars span:last-of-type svg').click()"));

Hopefully, this information is useful for you.
To escape JavaScript, you can do the following:

js.executeScript(StringEscapeUtils.escapeEcmaScript("document.querySelector('.js-react-thanks-button-answer-id-"+answerID+" button').click()"));
js.executeScript(StringEscapeUtils.escapeEcmaScript("document.querySelector('div[data-answer-id='" + answerID + "'] .sg-rate-box__filled-stars span:last-of-type svg').click()"));

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

Vue.js fails to display multiple uploaded images

I have been working on implementing a multiple image upload feature using vue.js. So far, everything seems to be functioning correctly. However, I am facing an issue where the HTML does not display thumbnails of the images I have selected. Additionally, I ...

Interactive real-time search results with clickable option through AJAX technology

Currently, I have implemented a live search feature that displays results based on what the user types in real time using the keyup function. However, one issue I encountered is that the displayed results inside the <div> tag are not clickable. Is th ...

Choose all checkboxes that have a certain identifier

Currently, I am developing a menu permission project using vue.js. Within this project, I have various submenus that are children of different menus. My objective is to automatically select all submenus under a selected menu when the user clicks on "select ...

What steps can I take to pinpoint the exact error location when running assetic:dump in Symfony2?

This error message indicates an issue with assetic:dump in Symfony2. [Assetic\Exception\FilterException] ...

The behavior of Selenium when waiting for ExpectedConditions.attributeToBe is not meeting our expectations

Currently, I am attempting to utilize a wait.until method on an element attribute in the following manner... public static void WaitForElementSize(WebElement element, WebDriver driver, int timeoutInSeconds) { if (timeoutInSeconds > 0) ...

What is the method for incorporating parameters into an array filter?

Imagine having an array containing multiple duplicate objects. How can we create a condition to specifically identify objects with certain criteria, such as matching IDs, duplicate status, and duplicate dates? The goal is to only display the object with th ...

Executing TestNG tests without the need of an XML file utilizing TestNG code

I have a set of tests ready, but I am facing an issue with running them. Currently, I am working in normal Java environment and to run multiple tests, I am using TestNG. However, I am unsure about how to write the code to run multiple tests using TestNG an ...

Ways to refresh the appearance of clothing in Three.js

Currently, I am diving into the world of Three.js. My latest project involves adapting a shader that I originally created in Shader Toy to be displayed on my own webpage. The shader itself is a procedural terrain where users can navigate using the WASD key ...

Set the datepicker to automatically show today's date as the default selection

The date picker field is functioning correctly. I just need to adjust it to automatically display today's date by default instead of 1/1/0001. @Html.TextBoxFor(model => model.SelectedDate, new { @class = "jquery_datepicker", @Value = Model.Selecte ...

Having trouble accessing VR Path Registry - Selenium Webdriver

While attempting to run a test case using Selenium and Java, I encountered an issue where the page did not fully load, and the following message appeared in my Eclipse console: Unable to read VR Path Registry I am unsure of what this message signifies. ...

Using Async functions with Node.js Express.js router

I've been trying to search on Google, but I can't seem to find a clear answer to this one... Is it possible to pass ES7 async functions to the Express router? For example: var express = require('express'); var app = express(); app.ge ...

Tips for showcasing messages in a .dust file with connect-flash and express-messages in a Node application

I am currently working with Nodejs, Expressjs, and Kraken. I have been trying to display a message when a product is added on the index page, but despite several attempts to configure it, the messages are still not appearing as expected. Below is my config ...

Starting the Android System Magnifier with a Click: A Step-by-Step Guide

Is there a way to incorporate a magnifying glass into an HTML page using jQuery or within an Android app? Ideally, it would be for a single picture. In my application, I am displaying an HTML file from my assets in a webview. The HTML page utilizes jQuery ...

Generate a pair of dates and organize them in chronological order

Working on an application where the database response lacks a direct timestamp, causing data to render differently each day and requiring me to use .reverse() to fix my charts. I aim to implement a permanent solution using sort(), but struggling due to the ...

The checkbox must be checked for the conditional form to appear, experiencing a Safari bug in version 12.2. No issues found in Chrome and Firefox browsers

My form includes a billing address section with a checkbox that automatically sets the shipping address to match the billing address. If the user unchecks the checkbox, another form will appear below for entering a different shipping address. The Checkbox ...

Enable direct download from external servers

I'm in search of a way to prompt a download (by right-clicking and selecting "save as") for both mp4 and flv files. The challenge is that these files are not hosted on my server, they are direct links from external websites. I have tried using .htacce ...

List of recommended countries with their respective flags and descriptive text

I am currently working on creating a country suggestion list that includes flags next to the selected result. While I have been successful in generating the suggestion box based on the country names, I am looking to enhance it by displaying the respective ...

Challenges with cross domain iframes

I am trying to find a way to send a message from an iframe to the parent page at regular intervals, for example: Iframe Domain = www.abc.com Parent Domain = www.xyz.com I have looked into the following resources: Cross domain iframe issue If anyone ha ...

Using JavaScript code to sift through and eliminate irrelevant data

Recently, I started learning about angular js and came across a link from which I need to extract a list of names and ids. I successfully retrieved the list in json format, but now I need to filter out unwanted items. The criteria for filtering is based ...

Is it feasible to establish a direct link between an Angular.js application and Dynamodb? Are there any other solutions available to eliminate the need for a backend

Personally, I believe that removing the backend could be a successful strategy for addressing sysadmin/scale issues. Do you agree with this approach? Is there a proven method for eliminating the backend in web applications that require database access? I& ...