如何使用硒获取元素颜色 [英] How to get element color with Selenium

查看:199
本文介绍了如何使用硒获取元素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查html页面中元素的颜色. 该元素的颜色是使用javascript设置的,请查看图像

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

具有div-id"Ab_banco_M1T1_switch"的元素可以采用4个值,当然根据"val"变量的值仅显示其中一个. val变量是通过服务器设置的,似乎脚本每X秒轮询一次服务器并更新val的值.

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.

我试图按如下方式获取元素的颜色:

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")

没有成功,它们将返回"null"或页面的backgorund颜色.

with no success, they return "null" or the backgorund-color of the page.

获得颜色的唯一方法是使用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)

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对元素进行了本地化,但是它并没有告诉我显示的颜色是红色还是另一种颜色,我只能通过查看网页才能知道它.

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.

换句话说,我想像Firebug一样访问当前显示的颜色,查看右侧的面板,您可以看到element.style ->background-Color = red.

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.

当我调用元素getCssCValue("background-color")时,我得到#body_right_div的backgorund颜色.

When I invoke element,getCssCValue("background-color") I get the backgorund-color of #body_right_div.

先谢谢您.

推荐答案

您可以通过以下方式获取元素颜色(元素的背景色):

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");

例如,如果要获取LinkedIn的登录"按钮的背景和文本颜色,代码如下:

For example if you want to get the background and text color of "Sign in" button for LinkedIn, the code is as follows:

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);

这篇关于如何使用硒获取元素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆