Java Selenium WebDriver 获取边框颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20535167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:35:45  来源:igfitidea点击:

Selenium WebDriver get border color

javacssextjsseleniumselenium-webdriver

提问by Mrunal Gosar

Hi all i am trying to get border color of an extjs 4.2 form control text field using getCssValue method. But i am not able to fetch it. it is returning me blank. Below is my code snippet u can try this as is.

大家好,我正在尝试使用 getCssValue 方法获取 extjs 4.2 表单控件文本字段的边框颜色。但我无法获取它。它让我空白。下面是我的代码片段,您可以按原样尝试。

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestClass 
{
    public static void main(String[] args) throws InterruptedException
    {
        WebDriver driver=new FirefoxDriver();
        Thread.sleep(2000);
        driver.get("http://docs.sencha.com/extjs/4.2.1/extjs-build/examples/form/dynamic.html");
        Thread.sleep(2000);
        WebElement element=driver.findElement(By.xpath(".//input[@name='first']"));
        Thread.sleep(2000);
        element.sendKeys("");
        element.sendKeys(Keys.TAB);
        Thread.sleep(2000);
        System.out.println("'"+element.getCssValue("border-color")+"'");
    }
}

Field, Xpath and CSS attribute highlighted in black border

字段、Xpath 和 CSS 属性以黑色边框突出显示

Webdriver version 2.33 (Java binding)

Webdriver 版本 2.33(Java 绑定)

FF 22

FF 22

采纳答案by Andrian Durlestean

enter image description hereHow to get border color or other css values look in Computedthere are all values that you can get:

在此处输入图片说明如何在Computed 中获取边框颜色或其他 css值,您可以获得所有值:

getCssValue("border-bottom-color")

returns rgba(209, 219, 223, 1) and need to clear it (this will work for rgba and rgb):

返回 rgba(209, 219, 223, 1) 并需要清除它(这适用于 rgba 和 rgb):

String rgb[] = driver.findElement(By.name("login[email]")).getCssValue("border-bottom-color").replaceAll("(rgba)|(rgb)|(\()|(\s)|(\))","").split(",");

Now our rgb is in array using this method to parse it

现在我们的 rgb 在数组中使用这个方法来解析它

String hex = String.format("#%s%s%s", toBrowserHexValue(Integer.parseInt(rgb[0])), toBrowserHexValue(Integer.parseInt(rgb[1])), toBrowserHexValue(Integer.parseInt(rgb[2])));

private static String toBrowserHexValue(int number) {
        StringBuilder builder = new StringBuilder(Integer.toHexString(number & 0xff));
        while (builder.length() < 2) {
            builder.append("0");
        }
        return builder.toString().toUpperCase();
    }

From this rgba(209, 219, 223, 1)we got this #D1DBDF

从这个rgba(209, 219, 223, 1)我们得到了这个#D1DBDF

P.S. Source of parsing int rgb to hex

PS解析int rgb到十六进制的来源

回答by Lt_Shade

There seems to be an issue with element.getCssValue("border-color") using a Firefox Driver. This is due to Shorthand CSS properties (e.g. margin, background, border) not been supported.

使用 Firefox 驱动程序的 element.getCssValue("border-color") 似乎存在问题。这是因为不支持速记 CSS 属性(例如边距、背景、边框)。

For Firefoxyou will need to enter

对于Firefox,您需要输入

System.out.println("'"+element.getCssValue("border-top-color")+"'");

The code will print out 'rgba(207, 76, 53, 1)'

代码将打印出'rgba(207, 76, 53, 1)'

Using a ChromeDriverto get your value.

使用ChromeDriver获取价值。

Your current code will print out 'rgb(207, 76, 53)'

您当前的代码将打印出'rgb(207, 76, 53)'

To set the ChromeDriver you might need to add this line before you declare your driver

要设置 ChromeDriver,您可能需要在声明驱动程序之前添加此行

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver.exe");
WebDriver driver=new ChromeDriver();

You can download the ChromeDriver from here http://chromedriver.storage.googleapis.com/index.html

您可以从这里下载 ChromeDriver http://chromedriver.storage.googleapis.com/index.html