我想从元素中获取样式值并将其分配给字符串变量。 我尝试使用以下代码,但当我尝试返回值“background:#ADFF2F”'时,它返回值“rgb(173, 255, 47)”:
IWebElement highlightedText = _driver.FindElement(By.Id("firstHeading")).FindElement(By.CssSelector("th-highlight-span"));
actualHighlightColour = highlightedText.GetAttribute("style");
这是我想从中获取样式值的网页的 src 代码:
<h1 id="firstHeading" class="firstHeading" lang="en">
::before
<th-highlight-span style="background:#ADFF2F" data-
rwstate="ss">California Proposition 218 (1996)</th-highlight-span>
我尝试使用的代码返回“rgb(173, 255, 47)”,可以在此处找到,但我想返回背景:#ADFF2F:
element.style {
background: #ADFF2F;
background-image: initial;
background-position-x: initial;
background-position-y: initial;
background-size: initial;
background-repeat-x: initial;
background-repeat-y: initial;
background-attachment: initial;
background-origin: initial;
background-clip: initial;
background-color: rgb(173, 255, 47);
最佳答案
将RGB转为十六进制
String hex = String.format("#%02x%02x%02x", r, g, b);
这将以小写字母返回十六进制,如果想要大写字母,则只需将 x
替换为 X
关于c# - 如何使用 selenium webdriver C# 获取元素的样式属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50505172/