getAttribute不返回硒中样式的完整值 [英] getAttribute not returning complete value for style in selenium

查看:85
本文介绍了getAttribute不返回硒中样式的完整值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下id元素上使用selenium getAttribute("style")方法:-

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">

,但API仅返回值的一半.它返回width: 250px; text-align: left; white-space: normal;,样式的剩余部分被裁剪.

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.

我正在尝试提取可见性的值,但是该方法未返回style的完整值.因此,我无法确定可见性的正确值.

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.

我执行了System.out.println("Style is:- "+super.getElement(NEXTAG_STORES_DIV).getAttribute("style"));

NEXTAG_STORES_DIV对应于id元素的xpath,super.getElement通过xpath提取元素

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

请帮帮我!

推荐答案

我刚刚使用Selenium 2.30.0进行了尝试,它工作正常,返回了整个属性.

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

尝试以下操作(所有示例均假设element是您需要测试的WebElement):

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

  1. 确保确实仅返回部分属性.您不只是将其打印到控制台中吗?许多控制台的线路长度有限.尝试将控制台设置为显示长行.以编程方式检查返回值的长度,或尝试评估

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

  • 如果可以,请尝试升级Selenium库.我不知道与属性获取有关的任何错误,但可能已经解决了一些问题(2.30.0版).

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

    在其他浏览器/操作系统/体系结构中尝试.如果它可以在某处工作,您将知道这是特定浏览器/驱动程序/操作系统/体系结构/其他问题,您可能可以将其集中精力进行修复或

    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.

    如果您只是想知道某个元素是否可见,则正确且通常首选的方法是调用

    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.

    如果style值在页面上动态更改(即,它不是静态地写在页面的源代码中),则WebDriver不能真正看到它,因为它没有获取动态更改.尝试通过JavaScript访问值:

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

  • 如果您实际上需要获取CSS的计算值 <浏览器实际使用的c10> 属性,而不是style属性中的属性(如果没有任何属性或以某种方式被覆盖),则需要使用JavaScript的这篇关于quirksmode.org的文章描述)是:

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

    同样,应通过

    String visibility = (String)js.executeScript(here_goes_the_whole_script, element);
    

  • 这篇关于getAttribute不返回硒中样式的完整值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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