1

I'm trying to evaluate the xpath to get the list of webelements using javascriptexecutor. i'm able to find the single element using (WebElement)jse.executeScript("return document.evaluate('//input[@name=\"q\"]' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;"); but when it comes to multiple set of elements it is throwing javascript error. can someone please help me in this. it will be lot helpful for me.

Code that i'm using to get the result

    try{
        Iterator<WebElement> arr=(Iterator<WebElement>)jse.executeScript("return document.evaluate('//a' ,document.body, null, XPathResult.ANY_TYPE, null)");
        int count=0;
        while(arr.hasNext()){
            count=count+1;
        }
        System.out.println(count);
        }catch(Exception e){
            e.printStackTrace();
        }

i have used all the types of XpathResult keywords.

ANY_TYPE
UNORDERED_NODE_ITERATOR_TYPE
ORDERED_NODE_ITERATOR_TYPE
UNORDERED_NODE_SNAPSHOT_TYPE
ORDERED_NODE_SNAPSHOT_TYPE
ANY_UNORDERED_NODE_TYPE

Error Stacktrace

org.openqa.selenium.JavascriptException: javascript error: error reading property
  (Session info: chrome=87.0.4280.88)
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'DESKTOP-S3RV3MH', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.88, chrome: {chromedriverVersion: 87.0.4280.88 (89e2380a3e36c..., userDataDir: C:\Users\DELL\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:61398}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 43d3c47a7d6d9419891c6948ef2398bd
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537)
    at demo.TestException.main(TestException.java:32)
4
  • 1
    Is there a specific reason you are not using the Java Selenium API findElement(s) methods? Commented Dec 13, 2020 at 15:10
  • Yes, I'm trying to build one small tool and for that i need this.. Commented Dec 13, 2020 at 15:24
  • Got it, but findElement(s) does exactly the same thing without the pain. The torture of this is not necessary. But, good luck to you. Commented Dec 13, 2020 at 15:29
  • Actually i need it to check that for xpath of the saved html file... At that time findelements not works as that is not natural web page. Commented Dec 13, 2020 at 15:31

1 Answer 1

2

use node type iterator, and add it to a javascript array and then return it :

An example code:

            String c ="results=document.evaluate('//a', document,null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);"+
            "var tagNames = [];\r\n" + 
            "while(node = results.iterateNext()) {\r\n" + 
            "  tagNames.push(node);\r\n" + 
            "};"
            + "\r\n return tagNames";
                        
            
            ArrayList<WebElement> arr=(ArrayList<WebElement>)((JavascriptExecutor) driver).executeScript(c);
            
            for (WebElement var : arr) 
            { 
                System.out.println(var.getAttribute("outerHTML"));
            }
Sign up to request clarification or add additional context in comments.

2 Comments

I will check it and update here in comment in a while..
Thank you .. It's working and i'm accepting your answer

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.