Search code examples
seleniumxpathphantomjsversionghostdriver

Which XPath version is supported in PhantomJS?


I'm using Selenium with PhantomJS. How can I find out which version of XPath is used in PhantomJS?


Solution

  • You can directly check whether specific functions are supported or not. For example, boolean() is provided by XPath 1.0, but abs() is only provided by XPath 2.0.

    PhantomJS 1.x & 2.0 only supports XPath 1.0.

    Full script:

    var page = require('webpage').create();
    
    console.log(JSON.stringify(page.evaluate(function(){
        var b = -1, body = -1, abs = -1;
        try{
            b = document.evaluate("boolean('a')", document, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
        }catch(e){}
        try{
            body = !!document.evaluate("//body", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        }catch(e){}
        try{
            abs = document.evaluate("abs(-10.5)", document, null, XPathResult.NUMBER_TYPE, null).numberValue;
        }catch(e){}
        return {
            "boolean": b,
            "body": body,
            "abs(-10.5)": abs,
        };
    }), undefined, 4));
    phantom.exit();
    

    Output:

    {
        "abs(-10.5)": -1,
        "body": true,
        "boolean": true
    }