How to get element color with Selenium

前端 未结 4 1819
走了就别回头了
走了就别回头了 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条回答
  •  温柔的废话
    2020-12-18 01:18

    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.

提交回复
热议问题