JavascriptExecutor.executeScript() returning String does not work

3,395 views
Skip to first unread message

Rostislav Matl

unread,
Mar 7, 2011, 11:37:56 AM3/7/11
to webdriver
I wrote following code to test executeScript():

String siteUrl = "http://www.seznam.cz";
WebDriver driver = new new FirefoxDriver();

driver.get(siteUrl);
JavascriptExecutor js = (JavascriptExecutor) driver;

String scriptReturningString = "SZN.idGenerator()";
String scriptResult = (String)js.executeScript(scriptReturningString);
out.println("script result: " + scriptResult);

driver.quit();

I always get "script result: null" in output.
When I call different JS function that does not return anything, it
works.
Can you see any mistake ?

I run Selenium 2.0b1.

bp

unread,
Mar 8, 2011, 10:38:30 AM3/8/11
to webdriver
Did you add a return?

such as "return SZN.idGenerator()"

Rostislav Matl

unread,
Mar 9, 2011, 5:11:48 AM3/9/11
to webdriver
Yes, I found it already. (It took whole day to get my message here).
Adding 'return' works.

Роман Анатольевич

unread,
Dec 30, 2013, 8:03:51 AM12/30/13
to webd...@googlegroups.com
Could you please help me with:

public static String runJS(String jsComman) {
JavascriptExecutor js;
   js = (JavascriptExecutor)driver;
   String scriptReturningString = jsComman; 
   String scriptResult = (String)js.executeScript(scriptReturningString); 
return "rez: " + scriptResult;
}

What I did wrong?

The return string is always is: rez: null

Thnaks

понедельник, 7 марта 2011 г., 18:37:56 UTC+2 пользователь Rostislav Matl написал:

Krishnan Mahadevan

unread,
Jan 2, 2014, 7:04:21 PM1/2/14
to webd...@googlegroups.com
Make sure your JS begins with "return". 

For e., 

String userAgent = runJS("return navigator.userAgent ;");
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/groups/opt_out.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
Reply all
Reply to author
Forward
0 new messages