简体   繁体   中英

How to get text from parent element and exclude text from children (C# 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>

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.");

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:

  • 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 s tarts with Google Link . See StringAssert.StartsWith() .
  • get the outerHTML of the parent's text and feed to an HTML Parser, like Html 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()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM