Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
668 views
in Technique[技术] by (71.8m points)

selenium - 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);
        positionToColumnName.add(columnName);
    }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...