Roger
Roger

Reputation: 2135

Selenium: Getting the value of elements in the dom when they are hidden

I am iterating a list of elements found ByClassName(). For each item in that list, I need to extract the text value of their child elements. I have seen some solutions for using javascript to get at the values, but I must be missing something. In this specific instance I have a span, and I need the text or innerhtml from it. I have verified with Firebug that there is text in it.

Upvotes: 0

Views: 1069

Answers (2)

Nashibukasan
Nashibukasan

Reputation: 2028

If all you want is to access the children elements you can also use xpath's //div[@class='Class']/child::div where you can change the classname and the child type. If you are wanting to access all children of the parent element you can return a list of elements that match your selector as below:

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

At this point, yes, if you can access the element you should be able to access the innerHTML. Note that I have written my code in C#, which I believe is what your code is in too.

Upvotes: 1

Roger
Roger

Reputation: 2135

It turns out GetAttribute("innerHTML") is not inhibited by hidden 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;
    }

Upvotes: 1

Related Questions