本文介绍了Selenium WebDriver获取边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有我正在尝试使用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)+');
}
}


Webdriver版本2.33(Java绑定)



FF 22

解决方案


如何获取边框颜色或其他css值在已计算中可以获得所有值:

  getCssValue(border-bottom-color)

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

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

现在我们的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();
}

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



PS


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.

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")+"'");
    }
}

Webdriver version 2.33 (Java binding)

FF 22

解决方案

How to get border color or other css values look in Computed there are all values that you can get:

getCssValue("border-bottom-color")

returns rgba(209, 219, 223, 1) and need to clear it (this will work for rgba and 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

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

P.S. Source of parsing int rgb to hex

这篇关于Selenium WebDriver获取边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:27