How to get text from parent element and exclude text from children (C # Selenium)

Is it possible to get text only from the 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 home page.</span> </a> ... </div> 

In C # (or any other 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, in the linktext there will be a link "Google LinkThis" will lead you to the Google homepage.

Without doing a lot of string manipulation (for example, getting the text of all the children and subtracting the parent from the resulting text), is there a way to get only the text from the parent?

+8
c # selenium selenium-webdriver
source share
1 answer

This is a common problem in selenium , because you cannot directly access text nodes - in other words, your XPath expressions and CSS selectors must point to the actual element.

Here is a list of possible solutions to your problem:

  • get the text of the parent for each child, get the text and remove it from the parent text. What you have left is the desired text - Google Link in your case.
  • if you want to get Google Link just to make a statement, you might be well checked if the parent text starts with Google Link . See StringAssert.StartsWith() .
  • Get outerHTML parent text and submit to an HTML parser, such as the Html Agility Pack . Something like that:

     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()); 
+6
source share

All Articles