Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read text from hidden element with Selenium WebDriver?

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?

like image 922
dokaspar Avatar asked Oct 24 '12 10:10

dokaspar


People also ask

How do I click hidden elements in Selenium WebDriver?

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. Javascript Executor is used to handle hidden elements on the page.

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

How do I get inner text in Selenium?

The Selenium WebDriver interface has predefined the getText() method, which helps retrieve the text for a specific web element. This method gets the visible, inner text (which is not hidden by CSS) of the web-element.


4 Answers

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/

like image 185
user3152984 Avatar answered Oct 06 '22 20:10

user3152984


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.

like image 30
specialscope Avatar answered Oct 06 '22 19:10

specialscope


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
like image 7
Tim Büthe Avatar answered Oct 06 '22 21:10

Tim Büthe


Try this

        WebElement hiddenElement  = GET YOUR ELEMENT HERE;
        String hiddenContent= hiddenElement.getAttribute("textContent");
like image 1
Abhigyan Avatar answered Oct 06 '22 19:10

Abhigyan