getAttribute not returning complete value for style in selenium getAttribute not returning complete value for style in selenium selenium selenium

getAttribute not returning complete value for style in selenium


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