如何从父元素获取文本并从子元素中排除文本(C#Selenium) [英] How to get text from parent element and exclude text from children (C# Selenium)
问题描述
是否只能从Selenium中的父元素而不是其子元素获取文本?
Is it possible to get the text only from a parent element and not its children in Selenium?
示例: 假设我有以下代码:
Example: Suppose I have the following code:
<div class="linksSection>
<a href="https://www.google.com/" id="google">Google Link
<span class="helpText">This link will take you to Google's home page.</span>
</a>
...
</div>
在C#(或任何语言)中,我将拥有:
In C# (or whatever language), I will have:
string linktext = driver.FindElement(By.CssSelector(".linksSection > a#google")).Text;
Assert.AreEqual(linkText, "Google Link", "Google Link fails text test.");
但是,链接文本将带有"Google链接,此链接会将您带到Google的主页."
However, the linktext will have "Google LinkThis link will take you to Google's home page."
不进行大量字符串操作(例如获取所有子项的文本并从父项的结果文本中减去文本),有没有办法从父项元素中仅获取文本?
Without doing a bunch of string manipulation (such as getting the text of all the children and subtracting that from resultant text of the parent), is there a way to get just the text from a parent element?
推荐答案
This is a common problem in selenium
since you cannot directly access text nodes - in other words, your XPath expressions and CSS selectors have to point to an actual element.
以下是您的问题的可能解决方案的列表:
Here is the list of possible solutions for your problem:
- 获取父元素的文本,对于每个孩子,获取文本并将其从父文本中删除.您剩下的就是所需的文本-在您的情况下为
Google Link
. - 如果您只是想作一个断言而获得
Google Link
,则可以通过检查父母的文本是否与Google Link
匹配.请参见StringAssert.StartsWith()
. -
获取父级文本的
outerHTML
并提供给HTML解析器,例如Html Agility Pack
.遵循以下原则:
- get the parent element's text, for each child, get the text and remove it from the parent's text. What you would have left is the desired text -
Google Link
in your case. - if you want to get the
Google Link
just to make an assertion, it could be that you would be okay with checking if the parent's text starts withGoogle Link
. SeeStringAssert.StartsWith()
. get the
outerHTML
of the parent's text and feed to an HTML Parser, likeHtml Agility Pack
. Something along these lines:
string outerHTML = driver.FindElement(By.CssSelector(".linksSection > a#google")).GetAttribute("outerHTML");
HtmlDocument html = new HtmlDocument();
html.LoadHtml(outerHTML);
HtmlAgilityPack.HtmlNode a = html.DocumentNode.SelectNodes("//a[@id='google']");
HtmlNode text = strong.SelectSingleNode("following-sibling::text()");
Console.WriteLine(text.InnerText.Trim());
这篇关于如何从父元素获取文本并从子元素中排除文本(C#Selenium)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!