从节点获取文本 [英] Getting text from a node
本文介绍了从节点获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一段这样的 HTML:
I have a piece of HTML like this:
<a href="/something">
Title
<span>Author</span>
</a>
我有一个与此 HTML 匹配的 WebElement.如何从中仅提取标题"?方法 .getText() 返回标题\n作者"...
I got a WebElement that matches this HTML. How can I extract only "Title" from it? Method .getText() returns "Title\nAuthor"...
推荐答案
你不能在 WebDriver API 中做到这一点,你必须在你的代码中做到这一点.例如:
You can't do this in the WebDriver API, you have to do it in your code. For example:
var textOfA = theAElement.getText();
var textOfSpan = theSpanElement.getText();
var text = textOfA.substr(0, textOfA.length - textOfSpan.length).trim('\n');
注意尾随的换行符实际上是 <a>
元素文本的一部分,所以如果你不想要它,你需要把它去掉.
Note that the trailing newline is actually part of the text of the <a>
element, so if you don't want it, you need to strip it.
这篇关于从节点获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文