Selenium Javascript executor returns null Selenium Javascript executor returns null selenium selenium

Selenium Javascript executor returns null


The problem is that you execute two statements in executeScript(). The function definition of tmp() and the function call of tmp().

I don't know the details, but the function definition seems to return null.

Since executeScript returns the first value that can be returned, it returns null. If you don't define the function and write the code inline, it will work.

JavascriptExecutor jsExec = (JavascriptExecutor) driver;Object inpType = jsExec    .executeScript("var attrb = jQuery(jQuery(\"[name='q']\")[0]).attr('type');"+            "if(typeof attrb !== 'undefined' && attrb !== false)" +            "{return attrb;}" +            "else{return '';}");System.out.println("-------------- Type: " + inpType);

This should print your expected value.

Edit: Also, your posted code doesn't escape the "" around [name='q']. This ends the string and causes syntax errors.


You should add a return statement to the result you want to return from inside the jsExec.executeScript(...)