最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Selenium Javascript executor returns null - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited May 28, 2016 at 17:26 Graham Russell 1,04714 silver badges25 bronze badges asked Jul 28, 2013 at 17:05 J28J28 1,1103 gold badges15 silver badges25 bronze badges 1
  • Have added my selenium code above, please have a look.Thanks. – J28 Commented Jul 29, 2013 at 3:10
Add a ment  | 

3 Answers 3

Reset to default 22

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论