14

I have the following JavaScript code returning null when ran through Selenium JavascriptExecutor. However, the same code when ran in Firefox developer console returned a value.

function tmp(){
    var attrb = jQuery(jQuery("[name='q']")[0]).attr('type');
    if(typeof attrb !== 'undefined' && attrb !== false){
        return attrb;
    } else {
        return '';
    }
}

tmp();

The below is my WebDriver code with the JS the same as above:

JavascriptExecutor jsExec = (JavascriptExecutor)driver;
Object inpType = 
       jsExec.executeScript("function tmp(){...}tmp();");
System.out.println("Type: " + inpType);

Above outputs null instead of "text" string. Any ideas?

1
  • Have added my selenium code above, please have a look.Thanks. Commented Jul 29, 2013 at 3:10

3 Answers 3

22

you need to use return tmp() instead of tmp() in executeScript() method. Find the related reference driver.executeScript() returns NullPointerException for simple javascript

Sign up to request clarification or add additional context in comments.

1 Comment

This works, however then pasting the snippet into browser console, it throws this error "Uncaught SyntaxError: Illegal return statement".
4

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

Comments

1

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.

2 Comments

Thanks for your response. I do have everything inline earlier, but it caused firefox to return an error which says "return not in a function" which is why i moved this snippet into a function tmp() and called. Coming to quotes, the above is a typo. The script is dynamically generated where i have escaped '"' in my code, for simplicity i copied the above javascript into the function call.
@Jsm executeScript() can have a return statement and does not throw an error. Why not just use the function inline for executeScript(), and wrapped in a function for direct entering in Firefox?

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.