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