How to read text from hidden element with Selenium WebDriver?

被刻印的时光 ゝ 提交于 2019-11-27 00:56:57
specialscope

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.

user3152984

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

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);
anirules twenty
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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!