如果元素在屏幕上物理上不可见,则 WebElement getText() 在 Firefox 中为空字符串 [英] WebElement getText() is an empty string in Firefox if element is not physically visible on the screen

查看:15
本文介绍了如果元素在屏幕上物理上不可见,则 WebElement getText() 在 Firefox 中为空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将 Firefox 升级到版本 19 后,我的 Selenium Webdriver 测试最近中断了.在我的几个测试中,我需要检索页面上但在浏览器窗口中不可见的元素,即我必须向右滚动才能看到它们.自从升级到 Firefox 19(我从 15 升级,所以这可能是 16 以来的问题)我只能检索我可以在浏览器窗口上看到的元素的文本.我的 xpaths 正确检索了所有元素,例如在我的以下代码中:

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);
    }
}

列表列"的大小为 38,但在我的浏览器窗口中,我只能看到 10 列而无需滚动,因此当我将列名放入我的对象时,我得到 10 个列名,然后全部为空白.

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: 
...
...

这在 Firefox 15 中对我非常有效,但现在已损坏.有没有人遇到过类似的事情并找到了解决方法?或者有没有办法滚动到一个元素"以强制滚动从而使其在屏幕上可见?

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?

推荐答案

我找不到有关 WebDriver 和/或 Firefox 的行为方式的答案.在我的 AUT 中,我有一个网格显示报告和屏幕上不可见的任何单元格/WebElements 我可以在 Html 中看到,它们似乎没有被隐藏,WebDriver 会承认它可以看到它们,但是我无法检索任何元素的值,即 getText、getAttribute 等.所以为了解决这个限制(不确定它是一个错误还是它的行为方式)我使用了以下 JavaScript 代码,它似乎已经解决了给我的问题:

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);
}

从 v16+ 开始,这绝对是 Firefox 行为的改变.希望其他人也能发现这有帮助.

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

这篇关于如果元素在屏幕上物理上不可见,则 WebElement getText() 在 Firefox 中为空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆