有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何验证SeleniumWebDriver中的文本颜色?

我有一个web应用程序

使用SeleniumWebDriver编写自动化脚本

当我选择一些文本时,我已经写了一些颜色代码

Now I want to check that color is present or not.

如何验证selenium webdriver脚本中的颜色代码


共 (5) 个答案

  1. # 1 楼答案

    //you can also try converting rgb into hex and verify...
    
    // Color RGB   
     color = driver.findElement(By.id("xxxxx")).getCssValue("color").trim();    
     System.out.println("RGB_Color: " + color);  
    
     //  RGB to HEX   
     String color_hex[];  
     color_hex = color.replace("rgba(", "").split(",");       
     String actual_hex = String.format("#%02x%02x%02x", Integer.parseInt(color_hex[0].trim()), Integer.parseInt(color_hex[1].trim()), Integer.parseInt(color_hex[2].trim()));  
    
    // further can verify with Actual hex value with Expected hex value  
    Assert.assertEquals("actual_hex should equal to: ", "#808080", actual_hex);
    
  2. # 2 楼答案

    您可以使用.getCssValue来获取颜色的值

    正如您指定的,如果您想验证颜色,您可以断言它,类似这样的东西

    assertTrue(selenium.isElementPresent("css=td[bgcolor=#000]"));
    
  3. # 3 楼答案

    在多次尝试不同的脚本之后,我终于找到了问题的答案

    String colorString = driver.findElement(By.id("foo")).getAttribute("class");
    String[] arrColor = colorString .split("#");
    assertTrue(arrColor[1].equals("FFFFFF"));
    

    谢谢大家帮助我

  4. # 4 楼答案

    绿色哈希代码#RRGGBB:#008000//测试数据,可替换为任何特定颜色

    可以尝试以下代码:

    String colorString = driver.findElement(By.id("foo")).getAttribute("class");
    String[] arrColor = colorString .split("#");
    assertTrue(arrColor[1].equals("008000"));
    
  5. # 5 楼答案

    WebElement eleSearch = driver.findElement(By.xpath("//*[@class='navsearchbar']//div[2]//div"));
    
    String rgbFormat = eleSearch.getCssValue("background-color");
    
    System.out.println(rgbFormat);     //In RGB Format the value will be print => rgba(254, 189, 105, 1)
    
    String hexcolor = Color.fromString(rgbFormat).asHex(); //converted Into HexFormat
    System.out.println(hexcolor);// Output of Hex code will be  => #febd69