如何使用 IWebElement 检索 P 标签的文本,包括任何嵌入的强标签?

How can I retrieve the text of a P tag INCLUDING any embedded strong tags using IWebElement?

我正在使用 Selenium WebDriver C# 的 NuGet 包。作为测试的一部分,我正在检查段落的文本。但是,该段落的 HTML 看起来像这样:

<p>This is <strong>bold</strong>.</p>

...如果我有一个 IWebElement 代表 p 标签,那么 .Text 属性 returns

This is .

换句话说,它只是 returns 来自 p 标签的文本,而不是来自嵌入的 strong 标签的文本。

IWebElement 上似乎没有任何方法或 属性 可以让我获得 p 标签及其子标签的全文。

所以...怎么办?

我现在不在办公室,但我的同事告诉我可以通过将 GetElementById 编辑的 IWebElement return 转换为 [=13 来解决问题=] 然后在上面调用 Text 属性。

这非常令人惊讶 - 我原以为 Text 会是虚拟的 属性,并且行为会由 运行 定义- time 类型,而不是 compile-time 类型。

更新

看来我的同事误会了。强制转换为 RemoteWebElement 没有解决问题。相反,似乎中断调试器并检查 Text 属性 导致它 return 正确的值。

我现在尝试在最小程序中重现此问题(见下文),但(惊奇!)我无法重现它。 Text 属性 的行为是正确的。我将继续研究我的实际设置有何不同。

namespace SeleniumTest
{
    using System;
    using System.Linq;

    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Support.UI;

    public class Program
    {
        public static void Main(string[] args)
        {
            const string ExamplePageUrl = "http://www.nngroup.com/consulting/ux-research-usability-testing/";

            var webDriver = new InternetExplorerDriver();

            webDriver.Navigate().GoToUrl(ExamplePageUrl);

            var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));

            wait.Until(w => w.Title == "Nielsen Norman Group: UX Research, Training, and Consulting");

            var paras = webDriver.FindElementsByTagName("p");

            var para = paras.FirstOrDefault(p => p.Text.Contains("We test your website or application"));

            if (para == null)
            {
                Console.WriteLine("Dang. Looks like the website changed.");
            }
            else
            {
                Console.WriteLine(para.Text);
            }

            Console.ReadLine();
        }
    }
}