java Selenium WebElement getCssValue 背景颜色被歪曲了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17728623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:47:42  来源:igfitidea点击:

Selenium WebElement getCssValue background colour misrepresented?

javaseleniumselenium-webdriver

提问by Husman

I am using Selenium Webdriver for automating functional testing on a javascript based web application. The javascript code (knockout.js) sets one of the OPTION elements for a SELECT dropdown lists to a different colour #FFFFCC, which is rgba(255, 255, 204, 1) (light yellow) (as verified by firebug).

我正在使用 Selenium Webdriver 在基于 javascript 的 Web 应用程序上自动进行功能测试。javascript 代码 (knockout.js) 将 SELECT 下拉列表的 OPTION 元素之一设置为不同的颜色 #FFFFCC,即 rgba(255, 255, 204, 1)(浅黄色)(经 firebug 验证)。

My code for use by selenium is as follows:

我供硒使用的代码如下:

    Select select = new Select(driver.findElement(By.id("views"))); // get the select
    List<WebElement> allOptions = select.getOptions(); // get all the options

    for (WebElement option : allOptions) { //iterate over the options
      if (option.getCssValue("background-color").compareToIgnoreCase("rgba(255, 255, 204, 1)") == 0) {
         // do something 
      }
    }

But that if statement always fails. When I do a system.out.println() to the value of option.getCssValue("background-color")it returns 'transparent' for all my option elements and 'rgba(51, 153, 255, 1)' for my modified option element, which when converted to hex is #3399FF (a blue colour).

但是 if 语句总是失败。当我option.getCssValue("background-color")对它的值执行 system.out.println() 时,它为我的所有选项元素返回 'transparent' 并为我修改后的选项元素返回 'rgba(51, 153, 255, 1)' ,当转换为十六进制时#3399FF(蓝色)。

Why is selenium reporting the incorrect hex value for this element?

为什么 selenium 报告此元素的十六进制值不正确?

采纳答案by Husman

I have a good theory about why this problem happens, and it seems reproducible.

关于为什么会出现这个问题,我有一个很好的理论,而且它似乎可以重现。

The blue color is the colour of the elements background when highlighted by a mouse. Selenium seems to pick up that colour instead of the actual background colour of the dropdown option.

蓝色是鼠标突出显示时元素背景的颜色。Selenium 似乎选择该颜色而不是下拉选项的实际背景颜色。

Perhaps i should report that as a bug or unintended feature to the devs.

也许我应该将其作为错误或意外功能报告给开发人员。