Using selenium, can I get the text of an invisible element? I tried to do it using driver.getElement().getText() but I am getting an empty string.
<p id="versionInfo" style="display: none;">
4.7.2<br/>
20130714-1512
</p>
</footer></body>
findElement(By.id("hidden_div")); String n = hiddenDiv. getText(); // does not work (returns "" as expected) String script = "return arguments[0]. innerText"; n = (String) ((JavascriptExecutor) driver). executeScript(script, hiddenDiv);
Mostly the hidden elements are defined by the CSS property style="display:none;". In case an element is a part of the form tag, it can be hidden by setting the attribute type to the value hidden. Selenium by default cannot handle hidden elements and throws ElementNotVisibleException while working with them.
We can interact with hidden elements in Selenium Webdriver. The hidden elements are the ones that are present in the DOM but not visible on the page. Mostly the hidden elements are defined by the CSS property style="display:none;".
Javascript is not necessary, obtain the value through the textContext attribute.
var text = driver.FindElement(By.Id("demo-div")).GetAttribute("textContent")
http://yizeng.me/2014/04/08/get-text-from-hidden-elements-using-selenium-webdriver/#c-sharp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With