Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using selenium, can I get the text of an invisible element?

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>
like image 432
djangofan Avatar asked Nov 15 '13 23:11

djangofan


People also ask

How do I getText from invisible element?

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);

How do you show hidden text in Selenium?

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.

Can Selenium interact with hidden elements?

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;".


1 Answers

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

like image 111
he_the_great Avatar answered Oct 13 '22 20:10

he_the_great