Tuesday, 4 August 2015

How to get css value in selenium webdriver using java

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetCssfontsizecolor {

    public static void main(String[] a) {

        // Initialize Firefox driver
        WebDriver driver = new FirefoxDriver();
        //Maximize browser window
        driver.manage().window().maximize();
        //Go to URL
        driver.get("https://www.google.com/");
        //Set selenium webdriver get timeout
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        String font_name, font_size, font_color;
        // Get font family name
        font_name = driver.findElement(By.partialLinkText("Gmail")).getCssValue("font-family").trim();
        // Get font size
        font_size = driver.findElement(By.partialLinkText("Gmail")).getCssValue("font-size").trim();
        // Get font color
        font_color = driver.findElement(By.partialLinkText("Gmail")).getCssValue("color").trim();

        System.out.println("font-family : " + font_name);
        System.out.println("font-size : " + font_size);
        System.out.println("font-color : " + font_color);

        // Browser close
        driver.close();

    }
}

No comments:

Post a Comment