Get color attribute from style sheet
I need to check the background color value of a div. Here's the HTML:
<div id="outercontainer" align="left"> Background color information is defined in the style.css file as follows:
#outercontainer { background-color: #EAEAEA; margin-left: auto; margin-right: auto; opacity: 1; width: 1000px; z-index: 2; } I tried to get the bgcolor value using the selenium.getattribute command, but selenium returned me the following error message:
ERROR: Could not find attribute of element: css = # oute rcontainer @ background-color in session bc60eb07f15e4e63986634fb59bf58a1
. 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."); } Actually, I tried different methods with different types of locators, but nothing helped me. What can you recommend?
I don't have a C # environment to test it, 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 rgba color. You will have to use Javascript since Selenium does not work on computed styles, only on styles defined in the HTML tags themselves.
I played with strings a bit to maintain readability: js string can be placed on one line.