0

我正在迭代找到 ByClassName() 的元素列表。对于该列表中的每个项目,我需要提取其子元素的文本值。我已经看到了一些使用 javascript 获取值的解决方案,但我一定遗漏了一些东西。在这个特定的例子中,我有一个跨度,我需要它的文本或 innerhtml。我已经用 Firebug 验证了里面有文字。

4

2 回答 2

1

事实证明 GetAttribute("innerHTML") 不受隐藏 html 的限制。

 public string GetChildElementTextByClassName(IWebElement element, string className)
    {
        string returnValue = string.Empty;

        try
        {
            IWebElement childElement = element.FindElement((By.ClassName(className)));
            if (childElement != null)
            {
                returnValue = childElement.GetAttribute("innerHTML").Replace(Environment.NewLine, "").Trim();
            }
        }
        catch (Exception)
        {

            //throw;
        }

        return returnValue;
    }
于 2013-02-13T16:18:58.387 回答
1

如果您只想访问子元素,您还可以使用 xpath //div[@class='Class']/child::div,您可以在其中更改类名和子类型。如果要访问父元素的所有子元素,可以返回与选择器匹配的元素列表,如下所示:

ReadOnlyCollection<IWebElement> elements = _driver.FindElements(By.XPath("//div[@class='ClassName']/child::div"));

在这一点上,是的,如果您可以访问元素,您应该能够访问 innerHTML。请注意,我已经用 C# 编写了我的代码,我相信这也是您的代码所在。

于 2013-02-13T21:22:14.310 回答