Retrieve the border color using Selenium WebDriver

Hey everyone, I'm currently trying to retrieve the border color of an extjs 4.2 form control text field using the getCssValue method. However, I'm having trouble getting the value as it's coming back blank. Below is a sample of my code that you can test out:

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

https://i.stack.imgur.com/PCLbp.jpg

WebDriver version 2.33 (Java binding)

Firefox 22

Answer №1

Learn how to retrieve and convert CSS values like border color in the Computed section, where all available values are displayed:

getCssValue("border-bottom-color")

When this code is executed, it returns rgba(209, 219, 223, 1) which may need to be converted (applicable for both rgba and rgb):

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

Now that we have our rgb values stored in an array, use the following method to parse them:

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();
    }

Converting from rgba(209, 219, 223, 1) gives us #D1DBDF

P.S. Refer to this source for more information on converting int rgb values to hex

Answer №2

Encountering a problem with element.getCssValue("border-color") while using a Firefox Driver is not uncommon. This issue arises from Shorthand CSS properties (such as margin, background, border) that are not supported.

For Firefox, the solution involves:

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

The above code snippet will output 'rgba(207, 76, 53, 1)'

If you switch to using a ChromeDriver to retrieve the value, your existing code would yield 'rgb(207, 76, 53)'

To configure ChromeDriver, you may need to include this line before initializing your driver:

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

You can obtain the ChromeDriver by downloading it from this link http://chromedriver.storage.googleapis.com/index.html

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the issue where my Android device is unable to receive string messages from a Winsock C++

I'm currently working on creating an Android app that connects to a C++ server running on Windows through TCP sockets. I've managed to successfully send a string from the app to the server, but encountering issues in receiving data back. I'v ...

What is the best way to responsively center an after pseudo-element above its parent?

Looking to create a dynamic tooltip without any fixed widths or constraints on the parent element. The process seems simple enough, but I'm facing an issue with centering the after element due to an existing transform attribute of transform: translat ...

CSS Animation: Smoothly transition out of a class

When this code is executed, the modal is displayed with a smooth transition using CSS3. Upon clicking on "Close Modal," the class ".is-active" is removed instantly. Instead, I would like to achieve the reverse effect where the class is removed gradually. ...

Issues with JQuery OnClick Function Detected

I am struggling with an issue related to the scripts in the <head> section of my website: <script src="scripts/jquery-1.11.0.min.js" type="text/javascript"></script> <script> $('#submit').click(function() { $('#submi ...

Having difficulty accessing object property using Chunk Template Engine

I am working on a project where I have created a list of objects and now need to retrieve the object name from a template using the Chunk Template Engine. The object list has been initialized as follows: html.set("items", new Item[]{new Item("Item 1", 2) ...

Error encountered: Unrecognized media in CSS validation and parsing issue

I've been having trouble validating my CSS and keep encountering error messages. Despite ensuring I have closing brackets, there are parse errors on line 39 and unrecognized media issues on lines 79 and 81. Additionally, lines 70 and 89 also trigger p ...

The input field within the mat-form has been styled to match the background of the page using CSS

I am currently facing an issue with the code below: Html: <form fxLayout="column" fxLayoutAlign="center center" [formGroup]="deleteForm" (ngSubmit)="onSubmitForm()"> <mat-form-field color="accent" appearance="outline"> <input ...

"Create sleek and stylish forms with Bootstrap's horizontal

I am attempting to create a horizontal form in Bootstrap using the code provided in their documentation. The example they show has input boxes filling the entire column space, but for some reason this is not happening when I try it, even after copying the ...

Is organizing a table into separate sections helpful?

I'm currently working on a personal project using Material UI, but I have a more general query regarding table implementation. I have designed a layout in Figma that I like, but I am struggling to translate it into code. https://i.sstatic.net/NoWEB.p ...

What is the method of communication between Selenium RC and the browser?

Can you explain the process by which selenium commands are sent to the browser? I understand that selenium proxies requests to URLs and injects the selenium core JS API into the response, but how exactly does a command like "click" reach the browser? In ...

Utilize Bootstrap columns to maximize vertical space efficiency

I recently came across a bootstrap layout like this: In this setup, there is a single .row that contains 8 .col-lg-3 divs. However, I noticed that there is excessive vertical whitespace present, which not only looks undesirable but also makes the layout a ...

Material UI Card shadow effect getting cropped

Currently experiencing an uncommon issue while using Material UI's Card component - the box-shadow is cut off on the top and bottom sides. Any suggestions on how to resolve this problem? Check out my code below: import React, { Component } from & ...

What steps should I take to address a situation in which a Protractor test becomes stuck indefinitely?

I've encountered an issue with a test case that was previously running successfully but is now getting stuck indefinitely during execution. This problem occurs each time the test attempts to click on a specific element, causing the test to hang withou ...

Height specification in Tailwind Grid

Hi there! I'm currently working on implementing a grid system in NextJS using Tailwind CSS. However, I've hit a roadblock when it comes to automatically sizing the grids to fit the parent element. Let me illustrate my issue with two images below ...

"Discovering the Selenium version using the webdriver: A step-by-step

Looking to determine the specific version of Selenium that the webdriver is communicating with. I recall using getEval and Selenium.version in the past, but unable to locate how to do it now. Any suggestions on how to accomplish this? ...

moving the symbol across the badge

I need the icon on the button to be adjusted slightly to the right. Button code <Button android:id="@+id/btn1" android:width="55sp" android:height="40sp" android:layout_width="wrap_content". ...

modify the appearance of HTML controls in real time with C#

Is there a way to dynamically add an additional navigation item to my website's menu list when the admin logs in through admin.aspx? The menu list is created using HTML controls such as <ul>...<li>, and I would like the new navigation item ...

Is it possible to bind browser keyboard scrolling to a div without explicit instructions?

Here is a question that closely relates to this one: Enable div to scroll by keyboard without clicking I am aware that explicitly focusing on the element will enable the desired behavior. My inquiry is whether there is a way to achieve this implicitly. ...

Looking to display a div with both a plus and minus icon? Having trouble getting a div to show with left margin? Need assistance hiding or showing div text

Can someone please review this source code? Here is the demo link: http://jsfiddle.net/bala2024/nvR2S/40/ $('.expand').click(function(){ $(this).stop().animate({ width:'73%', height:'130px' }); $( ...

What is the process for invoking a Java function in Android from JavaScript when using Vue?

My project is developed using Vue.js. In order to view it on a mobile phone, I decided to create an Android app. To achieve this, I am using the WebView component. However, when I click on an element in the Vue.js app, JavaScript calls Java and some events ...