I'm currently working on a selenium script that involves executing JavaScript code using the executeScript method. I've run into an issue when it comes to handling single (') and double quotes (") while passing variables.
Not Working:
js.executeScript("return document.querySelector('input[value=\" '"+group_name+"' \"]').value;").toString();
Working with static value:
js.executeScript("return document.querySelector('input[value=\" DemoTest \"]').value;").toString();
In the line
('input[value=\" '"+group_name+"' \"]')
, the group_name variable is enclosed by Java variable quotes.
Essentially, there are three quotes required for this passage:
1. Before the return statement " "
2. Within querySelector ''
3. When passing the variable value to querySelector ""
Please Note: The DOM only accepts these if they're passed through different quotes with inner strings 2 and 3.
However, I'm encountering a SyntaxError from the DOM that states
SyntaxError : missing ) after argument list
So how can I pass the variable in a way that the DOM will accept it with the required syntax?