如何从子文本节点中提取文本 - Selenium [英] How to extract text from child Text Nodes - Selenium
问题描述
我想从下面的代码中提取:我给你 0.00 欧元.".
From the code below I would like to extract: "I give you €0.00.".
使用以下 xpath,我可以从开发人员工具中定位它:
With the following xpath I can target it from Developer tools:
//*[@id="windowID"]/text()[2]
//*[@id="windowID"]/text()[2]
但是使用 Selenium 我只能用 driver.findElement(By.Xpath(""))
But using Selenium I can only look for this element with driver.findElement(By.Xpath(""))
问题是它说xpath表达式的结果不是一个元素,而是一个[object Text]
The problem is that it says that the result of xpath expression is not an element, but an [object Text]
如果我只针对祖先 div (windowID),则接收整个文本.我只想将提到的部分保存到一个字符串中.
If I target only the ancestor div (windowID), then the whole text is received. I only want the mentioned part to be saved to a string.
<div id="windowID" class="windowClass">"Your are awesome."<br>"I give you €0.00."<br>"But you should give me €0.00."<br>"Would you like to do that?"</div>
推荐答案
提取文本我给你€0.00.你可以使用以下代码行:
To extract the text I give you €0.00. you can use the following line of code :
IWebElement elem = driver.FindElement(By.XPath("//div[@class='windowClass' and @id='windowID']"));
string text = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].childNodes[3].textContent;", elem);
这篇关于如何从子文本节点中提取文本 - Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!