HTML
<div id='one'>
<button id='two'>i am button</button>
<button id='three'>i am button</button>
i am div
</div>
Run Code Online (Sandbox Code Playgroud)
码
driver.findElement(By.id('one')).getText();
Run Code Online (Sandbox Code Playgroud)
Jef*_*ffC 10
我已经看到这个问题在最后一年左右会弹出几次,我想尝试写这个功能......所以你走了.它接受父元素并删除每个子元素的textContent,直到剩下的是textNode.我已经在你的HTML上测试了这个并且它有效.
/**
* Takes a parent element and strips out the textContent of all child elements and returns textNode content only
*
* @param e
* the parent element
* @return the text from the child textNodes
*/
public static String getTextNode(WebElement e)
{
String text = e.getText().trim();
List<WebElement> children = e.findElements(By.xpath("./*"));
for (WebElement child : children)
{
text = text.replaceFirst(child.getText(), "").trim();
}
return text;
}
Run Code Online (Sandbox Code Playgroud)
你打电话给它
System.out.println(getTextNode(driver.findElement(By.id("one"))));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6010 次 |
| 最近记录: |