Selenium-确定透明HTML对象的背景色

Selenium-确定透明HTML对象的背景色,html,selenium,webdriver,background-color,Html,Selenium,Webdriver,Background Color,我想计算具有特定背景颜色的字符数。要了解我使用此解决方案遍历所有HTML节点,请执行以下操作: Html页面: <span style="background-color: #ffff00;"> <span id="child">I inherit the background color but Selenium gives me back transparent</span> </span> 现在的问题是,System.out

我想计算具有特定背景颜色的字符数。要了解我使用此解决方案遍历所有HTML节点,请执行以下操作:

Html页面:

  <span style="background-color: #ffff00;">
    <span id="child">I inherit the background color but Selenium gives me back transparent</span>
  </span>
现在的问题是,System.out将“透明”打印为css值,而不是将#ffff00打印为背景色

在本例中,我现在需要一些代码来查找父级的值。如果父级也有“透明”值,那么它应该继续这样做


我使用的是java 7,但可以按照Selenium执行JavaScript代码。

在CSS中,默认情况下值并不总是继承。在您的情况下,除非您明确要求,否则内部跨度不会继承背景色

为了证明这一点,我做了两个jsFiddles:

无继承:

与继承:

如您所见,如果您告诉子元素继承背景色,则返回的值仅与父元素的背景色相同

更新:

查看了添加的源代码后,您需要回答一些问题,但这是可能的

这是一个丑陋的算法,我面前没有selenium,所以您必须检查默认值。基本思想是通过DOM查看父/祖父母/曾祖父母,直到找到具有颜色集的父元素并返回它

public String getParentBackgroundColor(WebElement element) {
    WebElement current = element;
    //Ugly while true loop, fix this
    while(true) {
        //Get the current elements parent
        WebElement parent = element.findElement(By.xpath("..")); 

        //If the parent is null then doesn't have a parent so we should stop (change to seleniums default if element doesn't have a parent)
        if (parent == null) {
            throw new WeFailedException("Sorry, no parent elements had a background-color set");
        } else {
            //Otherwise get the parents color
            String color = parent.getCssValue("background-color");
            //If the color is transparent (based off your description, this could be some other default value) then set the parent as the current and continue the loop to try and get the parents parents color
            if (color == "transparent") {
                current = parent;
            } else {
                //If we found a color return it
                return color;
            }
        }
    }
}
然后,您可以使用它并将内部跨度传递给它,它将返回父颜色


希望这有帮助。

在CSS中,默认情况下,值并不总是继承的。在您的情况下,除非您明确要求,否则内部跨度不会继承背景色

为了证明这一点,我做了两个jsFiddles:

无继承:

与继承:

如您所见,如果您告诉子元素继承背景色,则返回的值仅与父元素的背景色相同

更新:

查看了添加的源代码后,您需要回答一些问题,但这是可能的

这是一个丑陋的算法,我面前没有selenium,所以您必须检查默认值。基本思想是通过DOM查看父/祖父母/曾祖父母,直到找到具有颜色集的父元素并返回它

public String getParentBackgroundColor(WebElement element) {
    WebElement current = element;
    //Ugly while true loop, fix this
    while(true) {
        //Get the current elements parent
        WebElement parent = element.findElement(By.xpath("..")); 

        //If the parent is null then doesn't have a parent so we should stop (change to seleniums default if element doesn't have a parent)
        if (parent == null) {
            throw new WeFailedException("Sorry, no parent elements had a background-color set");
        } else {
            //Otherwise get the parents color
            String color = parent.getCssValue("background-color");
            //If the color is transparent (based off your description, this could be some other default value) then set the parent as the current and continue the loop to try and get the parents parents color
            if (color == "transparent") {
                current = parent;
            } else {
                //If we found a color return it
                return color;
            }
        }
    }
}
然后,您可以使用它并将内部跨度传递给它,它将返回父颜色


希望这能有所帮助。

问题是,我想获取内部HTML对象的背景色,尽管它被设置为“透明”。内部元素的背景色是透明的,正如我的第一个JSFIDLE所证明的那样。由于您没有设置它,并且它没有继承父级颜色,因此它会作为您使用的函数的默认返回类型返回,在jQuery中,这是rgba(0,0,0,0),即一种空的透明黑色。这正是我的问题,在父级给出的示例中,我喜欢确定真实的背景颜色,使用SeleniumI并不认为您非常理解html和css。您的意思是要查找构成页面上内部对象所包含区域的实际渲染像素的背景色吗?因为这几乎是不可能的。一个简单的解决方法是将span{background color:inherit;}添加到css中(强制所有span继承其父级背景色,除非在样式中被覆盖),这样在我的第二个小提琴中它将返回您想要的值。我想要具有特定背景色的文本计数。即使它是某个内部元素。问题是我想要得到内部HTML对象的背景色,尽管它被设置为“透明”。内部元素的背景色是透明的,正如我的第一个JSFIDLE所证明的。由于您没有设置它,并且它没有继承父级颜色,因此它会作为您使用的函数的默认返回类型返回,在jQuery中,这是rgba(0,0,0,0),即一种空的透明黑色。这正是我的问题,在父级给出的示例中,我喜欢确定真实的背景颜色,使用SeleniumI并不认为您非常理解html和css。您的意思是要查找构成页面上内部对象所包含区域的实际渲染像素的背景色吗?因为这几乎是不可能的。一个简单的解决方法是将span{background color:inherit;}添加到css中(强制所有span继承其父级背景色,除非在样式中被覆盖),这样在我的第二个小提琴中它将返回您想要的值。我想要具有特定背景色的文本计数。即使它是某种内在因素。