【问题标题】:Selenium : Button color硒:按钮颜色
【发布时间】:2023-01-01 01:37:17
【问题描述】:
我正在尝试获取鼠标悬停前后按钮的颜色。我使用了以下代码。
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);
颜色更改后的值非常有效,但在我获得空白值之前。我很困惑,因为我对两个变量使用相同的代码。但是,一个返回一个值,一个不返回
【问题讨论】:
标签:
java
selenium-webdriver
selenium-chromedriver
background-color
getattribute
【解决方案1】:
- 没有为该元素定义的
color 属性。不是在悬停在该元素上之前也不是之后。在这两种情况下,您都可以采用 style 属性。
- 在悬停之前该元素中没有出现
style 属性,但是在悬停之后它出现了,所以在悬停之前在该元素上应用 .getAttribute("color") 或 .getAttribute("style") 是正确的。
- 要在悬停之前获取颜色,您可以应用
.getCssValue("background-color"),因为这是包含该颜色的特殊属性,您可以在此处看到
所以,请尝试以下代码:
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);