从styles表中获取color属性 [英] Get color attribute from the styles table
问题描述
我需要验证div的背景颜色的值。这里是HTML:
I need to verify the value of background color of div. Here's the HTML:
<div id="outercontainer" align="left">
有关背景颜色的信息在style.css文件中定义如下:
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;
}
我尝试使用 selenium .getattribute
命令,但是selenium向我返回了以下错误消息:
I tried to get the value of bgcolor using selenium.getattribute
command, but selenium returned me following error message :
错误:找不到元素属性:css =#oute
rcontainer @ background-color on session
bc60eb07f15e4e63986634fb59bf58a1
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?
推荐答案
我没有C#环境来测试它,这样的工作应该工作:
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);
现在 res
将包含 rgba
背景颜色的值。你将不得不使用Javascript,因为Selenium不工作的计算样式,只在HTML标签本身定义的样式。
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.
我使用linebreaks一点以保持可读性: js
字符串可以放在一行上。
I played with the linebreaks a bit to keep things readable: the js
string can all be put on a single line.
这篇关于从styles表中获取color属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!