I am trying to get the color of a button before and after mouse hower. I have used the following code.
driver.navigate().to("https://www.leafground.com/button.xhtml");
WebElement color = driver.findElement(By.xpath("//button[@id='j_idt88:j_idt100']//span[@class='ui-button-text ui-c']"));
String before = color.getAttribute("color");
Actions act = new Actions(driver);
act.moveToElement(color).perform();
String after = color.getAttribute("style");
System.out.println(before + " " + after);
The value after the color change works perfectly but before I am getting blank value. I am confused as I use the same code for both the variables. but, one returning a value and one doesn't
color
attribute defined for that element. Not before and not after the hovering over that element. Instead you can take style
attribute in both cases.style
attribute presented in that element, but after hovering it presented, so it's correct that you get nothing by applying .getAttribute("color")
or .getAttribute("style")
on that element before hovering..getCssValue("background-color")
as this is the special attribute containing that color as you can see hereSo, please try the following code:
driver.navigate().to("https://www.leafground.com/button.xhtml");
WebElement color = driver.findElement(By.xpath("//button[@id='j_idt88:j_idt100']//span[@class='ui-button-text ui-c']"));
String before = color.getCssValue("background-color");
Actions act = new Actions(driver);
act.moveToElement(color).perform();
String after = color.getAttribute("style");
System.out.println(before + " " + after);