如何从父元素获取文本并从子元素中排除文本(C#Selenium) [英] How to get text from parent element and exclude text from children (C# Selenium)

查看:217
本文介绍了如何从父元素获取文本并从子元素中排除文本(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:

  • 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());

这篇关于如何从父元素获取文本并从子元素中排除文本(C#Selenium)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆