WebElement getText() is an empty string in Firefox if element is not physically visible on the screen

后端 未结 2 1376
醉梦人生
醉梦人生 2020-12-04 01:11

My Selenium Webdriver tests broke recently after upgrading Firefox to version 19. In several of my tests I need to retrieve elements that are on the page but not visible on

2条回答
  •  盖世英雄少女心
    2020-12-04 01:37

    I couldn't find an answer as to why WebDriver and/or Firefox behave the way they do. In my AUT I have a grid that displays a report and any cells/WebElements that are not visible on the screen I'm able to see in the Html, they do not appear to be hidden and WebDriver will admit it can see them however I can not retrieve any of the element's values, i.e. getText, getAttribute, etc. So to work around this limitation (not sure if it's a bug or just the way it behaves) I used the following little bit of JavaScript which seems to have resolved the issue for me:

    private void scrollToElement(WebElement element){
    
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    }
    

    This was definitely a change in Firefox behavior from v16+. Hopefully someone else can also find this helpful.

提交回复
热议问题