How to get element color with Selenium

前端 未结 4 1823
走了就别回头了
走了就别回头了 2020-12-18 00:34

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

The element with div-id \"Ab_banco_M

4条回答
  •  -上瘾入骨i
    2020-12-18 01:07

    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:

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

提交回复
热议问题