As defined in WebDriver spec, Selenium WebDriver will only interact with visible elements, therefore the text of an invisible element will always be returned as an empty string.
However, in some cases, one may find it useful to get the hidden text, which can be retrieved from element's textContent, innerText or innerHTML attribute, by calling element.attribute('attributeName') or injecting JavaScript like return arguments[0].attributeName.
innerHTML will return the inner HTML of this element, which contains all HTML tags inside. For example, innerHTML for <div>Hello <p>World!</p></div> would be Hello <p>World!</p> instead of Hello World!.
textContent and innerText will only retrieve all text content of its descendants without any HTML tags.
textContent is a W3C-compliant textContent property1, but sadly is not supported by IE2.
innerText is not part of the W3C DOM specification and not supported by Firefox.
Here is a brief demonstration on how to get text from hidden elements using Selenium WebDriver .NET, Ruby and Python bindings.
Environment Tested: Mac OS Sierra, Ruby 2.3.1p112, Selenium 3.0.5, ChromeDriver 2.26, GeckoDriver 0.13 Chrome 55, Firefox 50.1, PhantomJS 1.9.8
C#
Environment Tested: Windows 7, Selenium 2.40.0, PhantomJS 1.9.7
Python
Environment Tested: Windows 7, Python 2.7.5, Selenium 2.41.0, PhantomJS 1.9.7 Linux Mint 16, Python 2.7.5+, Selenium 2.41.0, PhantomJS 1.9.7
Output
Demo div <p style="display:none">with a hidden paragraph inside.</p><hr><br> Demo div <p style="display:none">with a hidden paragraph inside.</p><hr><br>
Demo div with a hidden paragraph inside. Demo div with a hidden paragraph inside.