0

I'm trying to run a javascript function I wrote to collect all comments of an HTML site via the xpath(requirement).
The function pasted in a browser, ofc. without the 'return' statement, works perfectly fine.
But when executed via the selenium 'javascriptexecutor' it returns an empty array.
I know that you have to put the javascript statements into a " code "+ form but for the sake of readability I formatted my code like below.
I hope someone can help me with this:)

ChromeDriver driver = new ChromeDriver();
String url = "AprivateSamplePage";
driver.get(url);
JavascriptExecutor js = (JavascriptExecutor) driver;
String text = (String)  js.executeScript("return nodes =[];
xPathResult=document.evaluate('//comment()',document,null,XPathResult.ANY_TYPE, null);
nodes =[]; 
node = xPathResult.iterateNext();
while (node) {nodes.push(node.textContent);
node =  xPathResult.iterateNext();}nodes;").toString();
System.out.print(text);

And The Output looks like this:

Only local connections are allowed.
Okt 30, 2018 8:56:07 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Detected dialect: OSS
[]
Process finished with exit code 0
2
  • When posting code, please take a minute to use a beautifier like beautifier.io, etc. It makes the code easier to read and understand. Commented Oct 30, 2018 at 14:52
  • @JeffC thank you, did not know about this will use this in the future. Commented Nov 1, 2018 at 10:56

1 Answer 1

1

You are executing the script js.executeScript("return nodes =[];"); only. The rest of the script is ignored after that return statement. Hence you receive an empty array.

Regarding executeScript(String) javaDoc documentation, your script code is wrapped and executed as body of an anonymous function like this:

function f() {
    return nodes = [];
    xPathResult = document.evaluate('//comment()', document, null, XPathResult.ANY_TYPE, null);
    nodes = [];
    node = xPathResult.iterateNext();
    while (node) {
        nodes.push(node.textContent);
        node = xPathResult.iterateNext();
    }
    nodes;
}();

As you know, each script statement is separated by ";". As the first statement is a return statement, the function ends there and returns the empty array as result.

In your browser console, the script works as expected because it does not stop at the return statement, but prints out the finale statements' nodes; value.

You should move the return from the first to the last statement:

xPathResult = document.evaluate('//comment()', document, null, XPathResult.ANY_TYPE, null);
nodes = [];
node = xPathResult.iterateNext();
while (node) {
    nodes.push(node.textContent);
    node = xPathResult.iterateNext();
}

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

4 Comments

Okay do you know by chance how to work around this, since im getting the desired outcome in my browser console. If got it correct, javascriptExecutor requires a 'return' in the beginning....
The return is not required to be placed at the beginning. See my edits @BashLaneUp
Vielen vielen Dank! Thanks a lot!
When posting code, please take a minute to use a beautifier like beautifier.io, etc. It makes the code easier to read and understand.

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.