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

前端 未结 1 1961
别那么骄傲
别那么骄傲 2020-12-06 10:52

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:

相关标签:
1条回答
  • 2020-12-06 11:24

    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 starts 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());
      
    0 讨论(0)
提交回复
热议问题