简体   繁体   English

Selenium-如何从多个子节点中的一个子(文本)节点检索文本

[英]Selenium - How to retrieve text from a child (text) node among multiple child nodes

My HTML looks like : 我的HTML看起来像:

<div id="cc" class="col-md-6" _ngcontent-c12="">
  <h5 _ngcontent-c12="">Blah Blah</h5>
  My Answer
</div>

When I use : 当我使用时:

driver.findElement(By.id("cc")).getText()

It returns me "Blah Blah My Answer" 它返回我“ Blah Blah我的答案”

I need only "My Answer" to be returned. 我只需要返回“我的答案”即可。 Is there a way to do that through Selenium WebDriver? 有没有办法通过Selenium WebDriver做到这一点?

As per the HTML you have provided to retrieve the text My Answer you can use the following code block : 根据您提供的用于检索文本“ 我的答案”的HTML,可以使用以下代码块:

WebElement elem = driver.findElement(By.id("cc"));
String mytext = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].childNodes[2].textContent;", elem);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当子节点存在时,如何检索子节点的文本 - How to retrieve the text of a child node only if the child node exists 如何使用 Selenium 中的以下子项检索没有标签的元素的文本 - How to retrieve text of an element without tag using following child in Selenium 如何从AccessibilityNodeInfo中的所有子节点获取文本 - how to get text from all the child nodes in AccessibilityNodeInfo 如何查找所有子节点的文本 - How to find text of all child nodes 如何在 Selenium WebElement 中获取元素的所有子节点,包括文本节点? - How do I get all child nodes of an element , including text ones, in Selenium WebElement? Java如何仅从节点而不是从其子节点读取文本 - Java how to read text just from a node but not from his child Android,Firebase-尝试检索多个子节点时,数据库仅将单个子节点作为字符串返回 - Android, Firebase - database only returns single child node as string when trying to retrieve multiple child nodes 是否可以从文本文件中读取文件结构并使用 JAVA 跟踪父节点和子节点? - Is it possible to read a file structure from text file and keep track of parent node and child nodes USING JAVA? 如何使用xpath从子节点打印文本? - How to print text from child node using xpath? 如何使用 Selenium 和 Java 从 th 节点内的文本节点检索文本 Effective Date - How to retrieve the text Effective Date from the text node within th node using Selenium and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM