问题描述
我只想从网络元素中获取innerText.我只想从锚标记中获取名称".我可以访问下面的example(anchorElement)中与标记关联的webdriver元素.我尝试了anchorElement.getText()和anchorElement.getAttribute("innerText").两者都返回我名称,将Z排序为A".我应该在这里做什么?
I want to get only innerText from a webelement. I want to get only "Name" from the anchor tag.I have access to webdriver element associated with tag in below example(anchorElement).I tried anchorElement.getText() and anchorElement.getAttribute("innerText"). Both return me "Name, sort Z to A". What should I do here ?
<a id="am-accessible-userName" href="javascript:void(0);" class="selected">
Name
<span class="util accessible-text">, sort Z to A</span>
<span class="jpui iconwrap sortIcon" id="undefined" tabindex="-1">
<span class="util accessible-text" id="accessible-"></span>
<i class="jpui angleup util print-hide icon" id="icon-undefined" aria-hidden="true"></i>
</span>
</a>
推荐答案
一些Javascript可以仅选择子文本节点:
A bit of Javascript can pick out just the child text node:
RemoteWebDriver driver = ...
WebElement anchorElement = driver.findElement(By.id("am-accessible-userName"));
String rawText = (String) driver.executeScript(
"return arguments[0].childNodes[0].nodeValue;",
anchorElement);
因此, anchorElement
作为那里的 arguments [0]
传递到Javascript中.
So anchorElement
is passed into the Javascript as arguments[0]
there.
很显然 childNodes [0]
假定文本节点在哪里.如果那不安全,您也可以迭代childNodes,也许检查 childNode.nodeName ==="#text"
Clearly childNodes[0]
is assuming where the text node is. If that's not safe, you could iterate the childNodes too, perhaps checking for childNode.nodeName === "#text"
这篇关于仅从webelemnt获取内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!