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
CodePudding user response:
- There is no
color
attribute defined for that element. Not before and not after the hovering over that element. Instead you can takestyle
attribute in both cases. - Before hovering no
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. - To get the color before hovering you can apply
.getCssValue("background-color")
as this is the special attribute containing that color as you can see here
So, 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);