gpt4 book ai didi

java - Selenium WebDriver 获取边框颜色

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:18:38 25 4
gpt4 key购买 nike

大家好,我正在尝试使用 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

Webdriver 版本 2.33(Java 绑定(bind))

FF 22

最佳答案

enter image description here如何获取边框颜色或其他 css 值请查看 Computed 您可以获得所有值:

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)|(\\))","").split(",");

现在我们的 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) 我们得到了这个 #D1DBDF

附言Source of parsing int rgb to hex

关于java - Selenium WebDriver 获取边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20535167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com