我想检查html页面中元素的颜色.使用javascript设置此元素的颜色,查看图像
具有div-id"Ab_banco_M1T1_switch"的元素可以假设4个值,当然根据"val"变量的值仅显示其中一个值.val变量是以某种方式从服务器设置的,它表示脚本每隔X秒轮询一次服务器并更新val的值.
我试图获得元素的颜色如下:
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")
Run Code Online (Sandbox Code Playgroud)
没有成功,它们返回"null"或页面的backgorund颜色.
获得颜色的唯一方法是使用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会对元素进行本地化,但它不会告诉我显示的颜色是红色还是其他颜色,我只能通过查看网页来了解它.
换句话说,我想像Firebug那样访问当前显示的颜色,看看右侧的面板,你可以看到element.style ->background-Color = red.
当我调用元素时,getCssCValue("background-color")我得到了backgorund颜色#body_right_div.
先感谢您.