我想使用硒在html dom正文中阅读以下注释
<html>
<head class=.....>
<body>
<script>...</script>
<!--
Campaign Name: POC_SITE_MONETIZATION_DEALS_QA
Experience Name: SMBelow - jQuery Move w/ Tracking
-->
我尝试了以下两种方法:
1)
String injectedCampaign = WebDriverManager.getWebDriver().findElement(By.tagName("body")).getAttribute("InnerHTML");
2)
WebElement ele=WebDriverManager.getWebDriver().findElement(By.tagName("body"));
Object attr = ((JavascriptExecutor)WebDriverManager.getWebDriver()).executeScript("return arguments[0].attributes);", ele);
答案 0 :(得分:0)
使用此方法:
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));
您将获得理想的结果。