getAttribute not returning complete value for style in selenium

微笑、不失礼 提交于 2019-12-04 17:59:10

I just tried this with Selenium 2.30.0 and it works fine, the whole attribute is returned.

Try the following things (all the examples assume element is the WebElement you need to test):

  1. Make really sure only a part of the attribute is returned. Aren't you just printing it into console? Many consoles have a limited line length. Try setting your console to show long lines. Check programatically the length of the returned value, or try evaluating

    element.getAttribute("style").contains("visibility")
    
  2. Try upgrading your Selenium library, if you can. I am not aware of any bug related to attribute getting, but there might have been some which is now (with version 2.30.0) solved.

  3. Try it in a different browser / OS / architecture. If it works somewhere, you'll know it's an issue of a particular browser / driver / OS / architecture / whatever and you might be able to focus it down and either fix it or file a bug.

  4. If you simply want to know whether an element is visible or not, the correct and generally preferred way is to call

    element.isDisplayed()
    

    This method takes care of all the rules you might need to inspect in order to determine whether it actually is visible or not.

  5. If the style value changes dynamically on the page (i.e. it's not statically written in the source code of the page), WebDriver can't really see it as it doesn't pick up dynamic changes. Try accessing the value via JavaScript:

    if (!driver instanceof JavascriptExecutor) {
        throw new IllegalStateException("JavaScript not enabled for this driver!");
    }
    JavascriptExecutor js = (JavascriptExecutor)driver;
    String styleAttribute = (String)js.executeScript("return arguments[0].style", element);
    
  6. If you actually need to get the computed value of the CSS visibility attribute that is actually used by the browser and not the one in the style atribute (if there either isn't any or is somehow overridden), you need to use the JavaScript's getComputedStyle() method. One way (described by this article on quirksmode.org) is this:

    var elem = arguments[0];
    if (elem.currentStyle) {
        var vis = elem.currentStyle['visibility'];
    } else {
        var vis = document.defaultView.getComputedStyle(elem, null).getPropertyValue('visibility');
    }
    return vis;
    

    Again, this should be invoked via

    String visibility = (String)js.executeScript(here_goes_the_whole_script, element);
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!