How to read text from hidden element with Selenium WebDriver?

后端 未结 8 2167
难免孤独
难免孤独 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: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

提交回复
热议问题