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?
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.
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/
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
Try this
WebElement hiddenElement = GET YOUR ELEMENT HERE;
String hiddenContent= hiddenElement.getAttribute("textContent");
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);
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!
I recommand to use:
JavascriptExecutor js = (JavascriptExecutor)hiddenDiv;
String n=(String) js.executeScript("return document.getElementById('hidden_div').value;");
System.out.println(n);
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.
来源:https://stackoverflow.com/questions/13047056/how-to-read-text-from-hidden-element-with-selenium-webdriver