Selenium WebDriver - getCssValue()方法 [英] Selenium WebDriver - getCssValue() method

查看:1058
本文介绍了Selenium WebDriver - getCssValue()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am doing a exercise to use cssGetValue method to retrieve the value from a particular web element's CSS property.

我有2个问题:


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

  1. why the cssGetValue method returned value 13px, which web element does the method actually referenced. 1a. I want to get CSS property for section labeled as "By ID". how should I modify my code so I can get CSS property value for id="by-id" section?

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

I used driver.close() method, but it won't close the browser after the script finished. Please explain to me why driver.close() method didn't work in this case.

这是我的代码片段:

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();
}


}

}



推荐答案

是的,都是正确的。

通过Firebug可以找到 font-size 的屏幕截图。

Here's a screenshot of where to find font-size through Firebug.

由于ids应该是唯一的(至少对于这个页面)你不需要 findElements 来查找id by-id 的元素列表,然后循环,使用 findElement 直接获取元素。

Since the ids are supposed to be unique (at least for this page), you don't need findElements to find a list of elements with id by-id and loop through, instead, you use findElement to get the element directly.

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"));
    }
}

这篇关于Selenium WebDriver - getCssValue()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆