44

I'm trying to read the example String 1000 out of a hidden <div> like this:

<div id="hidden_div" style="visibility:hidden">1000</div>

I am aware that WebElement.getText() does not work on hidden elements in Selenium 2 (WebDriver), so I searched for solutions (like this one) and apparently the following code should work:

WebElement hiddenDiv = seleniumDriver.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);

But it doesn't work for me, the variable n always ends up as null. What am I doing wrong?

dokaspar
  • 6,795
  • 14
  • 59
  • 87

8 Answers8

88

Might be useful as well:

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').

element.getAttribute("textContent") worked for me.

See more details there -> http://yizeng.me/2014/04/08/get-text-from-hidden-elements-using-selenium-webdriver/

Vince Bowdren
  • 5,986
  • 2
  • 24
  • 48
user3152984
  • 901
  • 1
  • 7
  • 4
  • 4
    Woe, this is the answer folks! I was about to do the javascript in the accepted answer above, but tried this, and works perfectly. Much cleaner. – Patrick Karcher Oct 31 '15 at 01:09
  • 1
    element.getAttribute("textContent").trim() gave me exactly what I needed. – Bjinse Jan 28 '16 at 13:28
  • 2
    Good answer! I had better luck with innerText than textContent, because innerText combines multiple newlines and whitespace into one single space, while textContent doesn't. – frodo2975 Jun 27 '16 at 19:12
  • awesome sir. very much appreciated :) – GigaByte May 27 '19 at 16:58
27

EDIT: Oh this works.

String script = "return document.getElementById('hidden_div').innerHTML";

In firefox.

And so does this.

String script = "return arguments[0].innerHTML";

I tried as well but it does not seem to work with pure Javascript. Start the browser with Jquery as mentioned here. How to use JQuery in Selenium? and use following code for script.

String script = "return $('#hidden_div').text();";

This works.

Community
  • 1
  • 1
specialscope
  • 3,910
  • 3
  • 22
  • 22
  • 7
    According to the Selenium WebDriver FAQ: ": Why is it not possible to interact with hidden elements? A: Since a user cannot read text in a hidden element, WebDriver will not allow access to it as well. However, it is possible to use Javascript execution abilities to call getText directly from the element..." I ended up using jQuery as well since my app is built on jQuery. Not sure if I agree with the reasoning behind this decision expressed in the FAQ, but at least there is a way to get at hidden text. – Armchair Bronco Jan 09 '13 at 02:29
  • 3
    In older Selenium versions (at least 2.25), it was possible to get the text of a hidden element. In newer versions (at least 2.39), `getText()` returns an empty string if the element is not visible. This is in both cases different from the behavior when interacting with hidden elements. If you try to execute `click()` or `sendKeys(...)` you'll get an ElementNotVisibleException. – Tim Büthe Feb 10 '14 at 16:54
  • 2
    Just for completeness: The WebDriver FAQ can be read under https://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions . – sleske Feb 13 '14 at 14:24
  • How do I do this on `python` selenium? – Trect Jun 06 '19 at 17:13
  • @TimBüthe Thanks, this explains why an old test fails :) – JollyJoker Oct 22 '19 at 10:55
6

Building upon the work of the already given answers, I created this utility method (Java). Maybe this is helpful for someone else.

public static String getText(WebDriver driver, WebElement element){
    return (String) ((JavascriptExecutor) driver).executeScript(
        "return jQuery(arguments[0]).text();", element);
}
  • I use jQuery's text() to extract text nodes only. innerHTML would give you HTML tags as well.
  • I use jQuery instead of $ in case of noConflict
  • don't manipulate the element or it's visibility
Tim Büthe
  • 58,799
  • 16
  • 82
  • 126
1

Try this

        WebElement hiddenElement  = GET YOUR ELEMENT HERE;
        String hiddenContent= hiddenElement.getAttribute("textContent");
Abhigyan
  • 621
  • 10
  • 24
  • This doesn't seem to be any different to the answer from user3152984 https://stackoverflow.com/a/32475546/566763 – RedYeti Jan 18 '21 at 15:34
1

I recommand to use:

JavascriptExecutor js = (JavascriptExecutor)hiddenDiv;
String n=(String) js.executeScript("return document.getElementById('hidden_div').value;");
System.out.println(n);
BOB
  • 658
  • 2
  • 11
  • 31
0

I came across the same problem of reading invisible elements but I wanted back a WebElement object to do further parsing (not just its text).

This bit of code fulfilled my requirement.

(WebElement) ((JavascriptExecutor) driver).executeScript(
    "arguments[0].style[\"display\"] = \"block\";"+
    "arguments[0].style[\"visibility\"] = \"visible\";"+
    "return arguments[0];", 
element);
prav
  • 146
  • 5
0

I'm relatively new to Selenium (and to programming as whole), but I'm just sharing a solution that worked for me.

Selenium 2 was not designed for handling elements with hidden visibility directly. You won't be able to find it's ID or CSS Selector, for example.

I had a situation with a bot where I had a HTML table with lots of itens, and when clicking when of them, a dropdown with hidden visibility openned. It was even in another frame.

It's a specific situation, but I couldn't find any solution, so I chose this (bad) one, but that works really consistently, despite the ugly code.

First you should switchToDesiredFrame(); - enter your driver.switchTo.frame() logic here.

Than:

WebElement table = driver.findElements(By.tagName("table")).get(index_1);

List<WebElement> dataCells= table .findElements(By.tagName("td"));

WebElement spceificDataCellIWanted = dataCells.get(index_2);

System.out.println(spceificDataCellIWanted.getText());

The dataCells are literally the <td> tags, and they become WebElements in a list just as <td>'s are the elements in a list under the <table> "container".

It worked on Chrome and Firefox for me, but not on any headless browser, not sure exactly why. If you guys come across anything like that and have a more elegant solution (probably not so difficult to find it), please share!

Linh
  • 43,513
  • 18
  • 206
  • 227
-2
type='hidden'

When we come across any such case, first thing we should do is to try any action which doesn't make any change on that page, like drag etc, then do a frame switch.

Try a getText(), if that doesn't work, try the above as the 2nd alternative.

Leonardo Alves Machado
  • 2,481
  • 7
  • 31
  • 43