質問

I am using the selenium getAttribute("style") method on the following id element:-

<div id="ntsDiv_1" style="width: 250px; text-align: left; white-space: normal; top: 1090px; left: 131px; visibility: hidden;" class="mlt-pop-container">

but the API is returning only the half of the value. It is returning width: 250px; text-align: left; white-space: normal; and the remaning portion of the style is clipped.

I'm trying to extract the value of the visibility, but the method is not returning the complete value of style. Hence, i am unable to determine the correct value of visibility.

I executed System.out.println("Style is:- "+super.getElement(NEXTAG_STORES_DIV).getAttribute("style"));

NEXTAG_STORES_DIV corresponds to the xpath of the id element, and super.getElement extracts element by xpath

Please help me out!!

役に立ちましたか?

解決

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);
    
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top