これは JavaScript で行う必要があります。WebDriver (Firefox、Chrome など) がそれをサポートしている場合は、次のように実行できます。
public class Main
{
//@formatter:off
final static String JS_SCRIPT_GET_TEXT = "var element = arguments[0]; " +
"var text = ''; " +
"for (var i = 0; i < element.childNodes.length; i++) " +
" if (element.childNodes[i].nodeType === Node.TEXT_NODE) " +
" { " +
" text += element.childNodes[i].textContent + ' '; " +
" } " +
"return text; ";
//@formatter:on
public static void main(final String[] args)
{
final FirefoxDriver driver = new FirefoxDriver();
driver.get("http://en.wikipedia.org/wiki/HTML");
final List<WebElement> findElementsByCssSelector = driver.findElementsByCssSelector("#mw-content-text div");
final WebElement webElement = findElementsByCssSelector.get(0);
final String extractInnerText = extractInnerText(webElement, driver);
System.out.println("---------------------");
System.out.println("Seleniums .getText():\n" + webElement.getText());
System.out.println("\n\n---------------------");
System.out.println("Just the node text:\n" + extractInnerText);
}
public static String extractInnerText(final WebElement webElement, final WebDriver webDriver)
{
final JavascriptExecutor javascriptExecutor = (JavascriptExecutor) webDriver;
String webElementText = (String) javascriptExecutor.executeScript(JS_SCRIPT_GET_TEXT, webElement);
webElementText = webElementText.trim();
return webElementText;
}
}
この例の Html の場合:
<div class="dablink">
For the use of HTML on Wikipedia, see
<a href="/wiki/Help:HTML_in_wikitext" title="Help:HTML in wikitext">Help:HTML in wikitext</a>
.
</div>
それはあなたを印刷します:
---------------------
Seleniums .getText():
For the use of HTML on Wikipedia, see Help:HTML in wikitext.
---------------------
Just the node text:
For the use of HTML on Wikipedia, see .
それが必要だと思います。extractInnerText(..)
メソッドをすべての Web 要素に適用できます。