从样式表中获取颜色属性

Neo*_*ort 4 css selenium getattribute

我需要验证div的背景颜色的值.这是HTML:

<div id="outercontainer" align="left">
Run Code Online (Sandbox Code Playgroud)

有关背景颜色的信息在文件style.css中定义,如下所示:

#outercontainer {
    background-color: #EAEAEA;
    margin-left: auto;
    margin-right: auto;
    opacity: 1;
    width: 1000px;
    z-index: 2;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用selenium.getattribute命令获取bgcolor的值,但是selenium返回了以下错误消息:

错误:无法找到元素属性:css =#oute rcontainer @ background-color on session bc60eb07f15e4e63986634fb59bf58a1

作为结果.这部分代码:

try
{
     string atr_str = selenium.GetAttribute("css=#outercontainer@background-color");
     Console.WriteLine(atr_str);
}
catch (SeleniumException)
{
     Console.WriteLine("Color value was not got.");
}
Run Code Online (Sandbox Code Playgroud)

事实上,我尝试了不同类型的定位器的不同方式,但没有任何帮助我.你有什么建议吗?

jro*_*jro 5

我没有C#环境来测试它,但是这样的东西应该工作:

string js = "
    window.document.defaultView.getComputedStyle(
        window.document.getElementById('outercontainer'), null).
            getPropertyValue('background-color');
            ";
string res = selenium.GetEval(js);
Run Code Online (Sandbox Code Playgroud)

现在res将包含rgba背景颜色的值.您将不得不使用Javascript,因为Selenium不适用于计算样式,仅适用于HTML标记本身定义的样式.

我玩了一下换行符以保持可读性:js字符串可以全部放在一行上.