【问题标题】:get list of elements using document.evaluate using javascriptexecutor in java selenium在 java selenium 中使用 javascriptexecutor 获取使用 document.evaluate 的元素列表
【发布时间】:2020-12-13 15:02:09
【问题描述】:

我正在尝试评估 xpath 以使用 javascriptexecutor 获取 webelements 列表。我可以使用(WebElement)jse.executeScript("return document.evaluate('//input[@name=\"q\"]' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;"); 找到单个元素,但是当涉及到多个元素集时,它会引发 javascript 错误。有人可以帮我吗?这对我很有帮助。

我用来获取结果的代码

    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();
        }

XpathResult 的所有关键字我都用过。

ANY_TYPE
UNORDERED_NODE_ITERATOR_TYPE
ORDERED_NODE_ITERATOR_TYPE
UNORDERED_NODE_SNAPSHOT_TYPE
ORDERED_NODE_SNAPSHOT_TYPE
ANY_UNORDERED_NODE_TYPE

错误堆栈跟踪

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)

【问题讨论】:

  • 您没有使用Java Selenium API findElement(s) 方法是否有特定原因?
  • 是的,我正在尝试构建一个小工具,为此我需要这个..
  • 知道了,但是findElement(s) 做同样的事情没有痛苦。这种折磨是不必要的。但是,祝你好运。
  • 实际上我需要它来检查保存的 html 文件的 xpath...当时 findelements 不起作用,因为那不是自然网页。

标签: javascript java selenium selenium-webdriver


【解决方案1】:

使用节点类型迭代器,并将其添加到javascript数组中,然后返回:

示例代码:

            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"));
            }

【讨论】:

  • 我会检查它并在评论中更新。..
  • 谢谢你..它正在工作,我接受你的回答
猜你喜欢
  • 2021-03-05
  • 2022-01-18
  • 2017-03-23
  • 2015-09-18
  • 2021-09-29
  • 2017-03-09
  • 1970-01-01
  • 2015-04-09
相关资源
最近更新 更多