Get color attribute from the styles table

Deadly 提交于 2019-12-17 21:31:27

问题


I need to verify the value of background color of div. Here's the HTML:

<div id="outercontainer" align="left">

The information about background color is defined in file style.css like so:

#outercontainer {
    background-color: #EAEAEA;
    margin-left: auto;
    margin-right: auto;
    opacity: 1;
    width: 1000px;
    z-index: 2;
}

I tried to get the value of bgcolor using selenium.getattribute command, but selenium returned me following error message :

ERROR: Could not find element attribute: css=#oute rcontainer@background-color on session bc60eb07f15e4e63986634fb59bf58a1

as the result. This part of my code:

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

In fact I tried different ways with different types of locators, but nothing helped me. What can you advise to do?


回答1:


I don't have a C# environment to test it in, but something like this should work:

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

Now res will contain the rgba value of the background color. You will have to use Javascript since Selenium doesn't work on the computed styles, only on the styles defined in the HTML tags themselves.

I played with the linebreaks a bit to keep things readable: the js string can all be put on a single line.




回答2:


Try with:

string rgbaColor = yourElement.GetCssValue("background-color");


来源:https://stackoverflow.com/questions/8135080/get-color-attribute-from-the-styles-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!