Selenium WebDriver – getCssValue()方法

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

我有两个问题:

  1. 为什么cssGetValue方法返回值13px,哪个web元素执行实际引用的方法。 1A。 我想获得标记为“By 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 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的屏幕截图。

在此处输入图像描述

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

获取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(); 

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

}

输出:

鼠标hover前按钮的颜色:rgba(68,68,68,1)鼠标hover后按钮的颜色:rgba(34,34,34,1)按钮的字体大小11px字体按钮的重量700

价值是正确的。 您需要在dev-tools中访问计算部分

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

参考:-

在此处输入图像描述