1

I have an int variable in java like

int height = 10;

I want to use this java int type variable inside javascriptexecutor of selenium webdriver. I have tried in some ways but it shows error all time

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("var test = '${height}';")

or like this

jse.executeScript("var test = '<%=height%>';"

What is the way to use this int type variable inside javascriptexecutor of selenium webdriver?

1

2 Answers 2

1

You need to use the + operator to achieve it.

Use the code below:

WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
int height=10;
String script="alert('"+height+" book does not exist');"; 
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript(script); 
Sign up to request clarification or add additional context in comments.

4 Comments

Dear Shubham jain, its return the same error : .org.openqa.selenium.WebDriverException: unknown error: height is not defined. any other suggestion pls
FIrst of all you need to assigne the value as 10 to your int height as I had assiged in my code, I thing you have not assigned any value as 10 to your variable. If you have done this that may be your facing sometype concat related issue. Use my updated code and let me know.
this is my code written according to your suggestion: int height = 10; String h = "var test = '${"+height+"}';"; jse.executeScript("alert(h);"); now the error is :: org.openqa.selenium.WebDriverException: unknown error: h is not defined
thanks a lot for your suggestion ... and finally it works
0

solution is like this:

int height = 10;//java variable
jse.executeScript("var test = "+height+";" //called inside executor
+ "alert('height is '+test);");  // checked that java variable in an alert works

finally thanks to Shubham jain for his suggestion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.