Java 如何使用 Selenium 获取元素颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23220575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get element color with Selenium
提问by user835132
I want to check the color of an element in an html page.
The color of this element is set with a javascript, look at the image
我想检查 html 页面中元素的颜色。这个元素的颜色是用javascript设置的,看图
The element with div-id "Ab_banco_M1T1_switch" can assume 4 values, of course only one of them is displayed according to the value of "val" variable. The val variable is set from the server somehow, it seemes the script polls the server every X-seconds and update the value of val.
div-id为“Ab_banco_M1T1_switch”的元素可以取4个值,当然根据“val”变量的值只显示其中一个。val 变量是以某种方式从服务器设置的,脚本似乎每 X 秒轮询一次服务器并更新 val 的值。
I've tried to get the color of the element as follow:
我试图按如下方式获取元素的颜色:
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Ab_banco_M1T1_switch")));
element.getAttribute("background")
element.getAttribute("style")
element.getAttribute("background-color")
element.getCssValue("style")
element.getCssValue("color")
with no success, they return "null" or the backgorund-color of the page.
没有成功,他们返回“null”或页面的背景颜色。
The only way to get the color is to use the Xpath
/html/body/div/div[2]/div[2]/div[2]/div/div/div/div[3]/div (for red, if I want the green
/html/body/div/div[2]/div[2]/div[2]/div/div/div/div[2]/div)
获取颜色的唯一方法是使用 Xpath
/html/body/div/div[2]/div[2]/div[2]/div/div/div/div[3]/div (for red, if I want the green
/html/body/div/div[2]/div[2]/div[2]/div/div/div/div[2]/div)
But it is not what I want. Indeed, the Xpath localizes the element, but it doesn't tell me if the color displayed is red or another one, I can know it only by looking at the web page.
但这不是我想要的。实际上,Xpath 对元素进行了本地化,但它没有告诉我显示的颜色是红色还是其他颜色,我只能通过查看网页才能知道。
In other words, I would like to access the current displayed color as Firebug does, look at the panel on right side, you can see that element.style ->background-Color = red
.
换句话说,我想像 Firebug 一样访问当前显示的颜色,查看右侧的面板,您可以看到element.style ->background-Color = red
.
When I invoke element,getCssCValue("background-color")
I get the backgorund-color of #body_right_div
.
当我调用 element 时,getCssCValue("background-color")
我得到了#body_right_div
.
Thank you in advance.
先感谢您。
回答by olyv
With XPath you can try to search by attribute. For example //div[@style='background: red']
. If you want to get color then for CSS I would use method getCSSValue()
使用 XPath,您可以尝试按属性进行搜索。例如//div[@style='background: red']
。如果你想获得颜色,那么对于 CSS,我会使用getCSSValue()方法
回答by Ripon Al Wasim
You can get the element color(Background color of element) by:
您可以通过以下方式获取元素颜色(元素的背景颜色):
element.getCssValue("background-color");
You can get the element text/caption color by:
您可以通过以下方式获取元素文本/标题颜色:
element.getCssValue("color");
For example if you want to get the background and text color of "Sign in" button for LinkedIn, the code is as follows:
比如你想获取LinkedIn的“登录”按钮的背景和文字颜色,代码如下:
driver.get("https://www.linkedin.com/");
String buttonColor = driver.findElement(By.name("submit")).getCssValue("background-color");
String buttonTextColor = driver.findElement(By.name("submit")).getCssValue("color");
System.out.println("Button color: " + buttonColor);
System.out.println("Text color " + buttonTextColor);
回答by Fai Hasan
try this, worked perfect for me:
试试这个,对我来说很完美:
import org.openqa.selenium.support.Color;
String color = driver.findElement(By.xpath("xpath_value")).getCssValue("color");
System.out.println(color);
String hex = Color.fromString(color).asHex();
System.out.println(hex);
So you can get color using getCssValue("color");
and getCssValue("background-color");
.
However, that would be in RGB, so you need to convert to hex. With hex code you can then compare the value and print it out so that you can see what you are getting after each getCssValue
.
所以你可以使用getCssValue("color");
和 获取颜色getCssValue("background-color");
。但是,那将是 RGB,因此您需要转换为十六进制。使用十六进制代码,您可以比较值并将其打印出来,以便您可以看到每个getCssValue
.
回答by Nagarjuna Yalamanchili
Try below code
试试下面的代码
public String ColorVerify(String cssSelector, String cssValue)
{
/* This method used to verify color code*/
WebElement target = driver.findElement(By.cssSelector(cssSelector));
String colorCode= target.getCssValue(cssValue);
String hexacolor = Color.fromString(colorCode).asHex();
return hexacolor;
}