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


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 the browser window, i.e. I would have to scroll right to see them. Since upgrading to Firefox 19 (I upgraded from 15 so this could be an issue since 16) I can only retrieve text for elements that I can see on the browser window. My xpaths properly retrieve all elements, for instance in my following code:

    private void buildColumnsMap(){
allColumnHeaders = new HashMap<String,Integer>();
positionToColumnName = new ArrayList<String>();
WebElement columnsRoot = driver.findElement(By.xpath(COLUMNS_ROOT_XPATH));
List <WebElement> columns = columnsRoot.findElements(By.xpath("./td/div/span"));
System.out.println("Number of columns found: " + columns.size());
for(int i = 0; i < columns.size(); ++i){
String columnName = columns.get(i).getText();
System.out.println("Column been inserted: " + columnName);
allColumnHeaders.put(columnName, i);

The list “columns” has a size of 38 but in my browser window I can only see 10 columns without having to scroll so when I go to put the column names into my objects I get 10 column names then all blanks.

Number of columns found: 38
Column been inserted: Date/Time
Column been inserted: Endpoint1
Column been inserted: Endpoint2
Column been inserted: Duration
Column been inserted: Codec1
Column been inserted: Codec2
Column been inserted: Avg MOS1
Column been inserted: Avg MOS2
Column been inserted: Avg Latency1 (ms)
Column been inserted: Avg Latency2 (ms)
Column been inserted: Avg Jitter1 (ms)
Column been inserted:
Column been inserted:

This worked for me perfectly fine in Firefox 15 but is now broken. Has anyone ran into something similar and found a work around? Or is there a way to “scroll to an element” as to force the scrolling and therefore make it visible on the screen?

  • You must to post comments

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.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.