Selenium UI 舉例 getCssValue


selenium jar包中,在WebElement的接口中,

String getCssValue(String var1);

可以通過標簽,獲取對應的css值。具體要怎么用呢,如下:

WebElement baidu = driver.findElement(By.id("su"));

su.getCssValue("font-size")
package main.java.aTestDirectory;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class TestBaiduElement {

    String url = "http://www.baidu.com/";

    @Test
    public void TestElement() {
        System.out.println("------Begin--------------");
        System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get(url);

        (new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver dr) {
                int index = dr.getPageSource().indexOf("百度一下");
                if (index != -1) {
                    return true; // 找到,退出等待
                } else {
                    return false; // 未找到,繼續等待
                }
            }
        });


        WebElement baidu = driver.findElement(By.id("su"));
        System.out.println("border-bottom-color = " + baidu.getCssValue("border-bottom-color"));
        System.out.println("border-bottom-style = " + baidu.getCssValue("border-bottom-style"));
        System.out.println("border-bottom-width = " + baidu.getCssValue("border-bottom-width"));
        System.out.println("box-sizing = " + baidu.getCssValue("box-sizing"));
        System.out.println("color = " + baidu.getCssValue("color"));
        System.out.println("display = " + baidu.getCssValue("display"));
        System.out.println("text-align = " + baidu.getCssValue("text-align"));
        System.out.println("background = " + baidu.getCssValue("background"));
        System.out.println("line-height = " + baidu.getCssValue("line-height"));
        System.out.println("height = " + baidu.getCssValue("height"));
        System.out.println("font-family = " + baidu.getCssValue("font-family"));
        System.out.println("font-size = " + baidu.getCssValue("font-size"));
        System.out.println("margin-bottom = " + baidu.getCssValue("margin-bottom"));
        System.out.println("margin-top = " + baidu.getCssValue("margin-top"));
        System.out.println("margin-left = " + baidu.getCssValue("margin-left"));
        System.out.println("margin-right = " + baidu.getCssValue("margin-right"));


        //System.out.println(baidu.getText());
        driver.quit();
        System.out.println("------End--------------");
    }
}
 
        

  



打印出來的結果:

 

通過Chrome校驗下,注意要點擊Computed下屬性才能看出:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM