如何在Selenium中验证Web元素的颜色和背景颜色?
我们可以借助getCSSValue()方法来验证Selenium中Web元素的颜色和背景颜色。
示例
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class CssColorValue { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://tutorialspoint.com/index.htm"; driver.get(url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //getting color attribute with getCssValue() String colr = driver.findElement(By.xpath("//*[text()=’GATE Exams’]")) .getCssValue("color"); //getting background color attribute with getCssValue() String bckgclr = driver.findElement(By.xpath("//*[text()=’ ’GATE Exams’]")) .getCssValue("background-color"); System.out.println(colr); System.out.println(bckgclr); driver.close(); } }
广告