Java 如何在selenium中读取html dom体中的注释

Java 如何在selenium中读取html dom体中的注释,java,html,selenium,Java,Html,Selenium,我想使用selenium在html dom正文中阅读下面的注释 <html> <head class=.....> <body> <script>...</script> <!-- Campaign Name: POC_SITE_MONETIZATION_DEALS_QA Experience Name: SMBelow - jQuery Move w/ Tracking --> (二) 使用此方法: public sta

我想使用selenium在html dom正文中阅读下面的注释

<html>
<head class=.....>
<body>
<script>...</script>
<!--
Campaign Name: POC_SITE_MONETIZATION_DEALS_QA
Experience Name: SMBelow - jQuery Move w/ Tracking
-->
(二)

使用此方法:

public static String printCommentedString(WebDriver driver, WebElement element) {

        return (String) ((JavascriptExecutor) driver).executeScript("return Array.prototype.slice.call(arguments[0].childNodes)\n" + 
                "    .filter(function(e) {\n" + 
                "        return e.nodeType === 8\n" + 
                "    })\n" + 
                "    .map(function(e) {\n" + 
                "        return e.nodeValue.trim()\n" + 
                "    })\n" + 
                "    .join('\\n');", element);
    }
然后在测试用例中:

        WebElement bodyElement = driver.findElement(By.xpath("//body"));
        System.out.println(printCommentedString(driver, bodyElement));

你会得到你想要的结果

是您提供的HTML,实际的HTML是实际的HTML,还是您为了简单起见对其进行了裁剪?它是实际的HTML snnipets只是脚本中的内容,头标记不显示父标记,脚本中的内容是管理的。请考虑更新问题
public static String printCommentedString(WebDriver driver, WebElement element) {

        return (String) ((JavascriptExecutor) driver).executeScript("return Array.prototype.slice.call(arguments[0].childNodes)\n" + 
                "    .filter(function(e) {\n" + 
                "        return e.nodeType === 8\n" + 
                "    })\n" + 
                "    .map(function(e) {\n" + 
                "        return e.nodeValue.trim()\n" + 
                "    })\n" + 
                "    .join('\\n');", element);
    }
        WebElement bodyElement = driver.findElement(By.xpath("//body"));
        System.out.println(printCommentedString(driver, bodyElement));