How to read text from hidden element with Selenium WebDriver?

后端 未结 8 2140
难免孤独
难免孤独 2020-11-27 15:38

I\'m trying to read the example String 1000 out of a hidden

like this:

相关标签:
8条回答
  • 2020-11-27 15:43

    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);
    
    0 讨论(0)
  • 2020-11-27 15:45

    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/

    0 讨论(0)
  • 2020-11-27 15:46

    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!

    0 讨论(0)
  • 2020-11-27 15:50

    Try this

            WebElement hiddenElement  = GET YOUR ELEMENT HERE;
            String hiddenContent= hiddenElement.getAttribute("textContent");
    
    0 讨论(0)
  • 2020-11-27 15:56

    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
    0 讨论(0)
  • 2020-11-27 15:58
    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.

    0 讨论(0)
提交回复
热议问题