6

我正在练习使用 cssGetValue 方法从特定 Web 元素的 CSS 属性中检索值。

我有两个问题:

  1. 为什么 cssGetValue 方法返回值 13px,该方法实际引用了哪个 web 元素。1a。我想获取标记为“按 ID”的部分的 CSS 属性。我应该如何修改我的代码,以便我可以获得 id="by-id" 部分的 CSS 属性值?

  2. 我使用了 driver.close() 方法,但脚本完成后它不会关闭浏览器。请向我解释为什么 driver.close() 方法在这种情况下不起作用。

    这是我的代码片段:

    package wd_findElementBy;
    
    import java.util.List;
    
    import org.junit.Test;
    
    import org.junit.Before;
    
    import org.junit.After;
    
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.WebElement;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    
    public class SearchWebElements 
    {
    
    WebDriver driver = new FirefoxDriver();
    private String baseUrl= "http://docs.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example";
    
    @Test
    public void findElements(){
    driver.get(baseUrl);
    
    try{
        List<WebElement> elements = driver.findElements(By.id("by-id"));
        System.out.println("number of elements: " + elements.size());
    
        for(WebElement ele : elements){
            System.out.println(ele.getTagName());
    
            System.out.println("get the text for web element with id='by-id' ");
            System.out.println("------------------------------------------------------------");
            System.out.println(ele.getText());
            System.out.println("------------------------------------------------------------");
            System.out.println(ele.getAttribute("id"));
            System.out.println(ele.getCssValue("font-size"));
    
        }
    }
    
    finally{
        //driver.close();
        driver.quit();
    }
    
    
    }
    
    }
    
4

4 回答 4

13

是的,都正确。

font-size这是通过 Firebug在哪里找到的屏幕截图。

在此处输入图像描述

由于 id 应该是唯一的(至少对于此页面而言),因此您无需findElements查找具有 id 的元素列表by-id并循环遍历,而是使用findElement直接获取元素。

try{
        WebElement byId = driver.findElement(By.id("by-id"));

        System.out.println(byId.getTagName());

        System.out.println("get the text for web element with id='by-id' ");
        System.out.println("------------------------------------------------------------");
        System.out.println(byId.getText());
        System.out.println("------------------------------------------------------------");
        System.out.println(byId.getAttribute("id"));
        System.out.println(byId.getCssValue("font-size"));
    }
}
于 2013-06-23T21:15:40.510 回答
11

获取 CSS 值:

driver.findElement(By.id("by-id")).getCssValue("font-size");//similarly you can use other CSS property such as background-color, font-family etc.

完成脚本执行后退出/关闭浏览器:

driver.quit();
于 2014-01-23T11:21:52.677 回答
1

公共类 GetCssValues {

public WebDriver driver;
private By bySearchButton = By.name("btnK");

@BeforeClass
public void setUp() {
    driver = new FirefoxDriver();
    driver.get("http://www.google.com");
}

@Test(priority=1)
public void getCssValue_ButtonColor()  {
    WebElement googleSearchBtn = driver.findElement(bySearchButton); 
    System.out.println("Color of a button before mouse hover: " + googleSearchBtn.getCssValue("color"));
    Actions action = new Actions(driver);
    action.moveToElement(googleSearchBtn).perform();
    System.out.println("Color of a button after mouse hover : " + googleSearchBtn.getCssValue("color"));
}

@Test(priority=2)
public void getCssValue_ButtonFontSize() {
    WebElement googleSearchBtn = driver.findElement(bySearchButton);
    System.out.println("Font Size of a button " + googleSearchBtn.getCssValue("font-size"));
}

@Test(priority=3)
public void getCssValue_ButtonFontWeight(){
    WebElement googleSearchBtn = driver.findElement(bySearchButton);
    System.out.println("Font Weight of a button "   +getFontWeight(googleSearchBtn) );
}

public String getFontWeight(WebElement element) {
    //Output will return as 400 for font-weight : normal, and 700 for font-weight : bold
    return element.getCssValue("font-weight");
}

@AfterClass
public void tearDown() {
    driver.quit();
}

}

输出:

鼠标悬停前按钮的颜色:rgba(68, 68, 68, 1) 鼠标悬停后按钮的颜色:rgba(34, 34, 34, 1) 按钮的字体大小 11px 按钮的字体粗细 700

于 2015-06-10T09:56:54.050 回答
0

值是正确的。您需要访问开发工具中的计算部分

在中添加属性名称getCssValue以获取有关相同的信息

一些例子是 :-

        System.out.println("font-size = "+ele.getCssValue("font-size"));
        System.out.println("background = "+ele.getCssValue("background"));
        System.out.println("line-height = "+ele.getCssValue("line-height"));
        System.out.println("color = "+ele.getCssValue("color"));
        System.out.println("font-family = "+ele.getCssValue("font-family"));

参考:-

在此处输入图像描述

于 2017-10-24T09:56:22.023 回答