【问题标题】:Selenium WebDriver - getCssValue() methodSelenium WebDriver - getCssValue() 方法
【发布时间】:2013-06-19 18:53:44
【问题描述】:

我正在练习使用 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();
    }
    
    
    }
    
    }
    

【问题讨论】:

  • getCssValue("font-size") 给你 13px 是正确的。这是&lt;div class="section" id="by-id"&gt; 的字体大小。你能指望什么?理想情况下,ID 是唯一的,您不需要 driver.findElements 并循环遍历。
  • @user1177636 感谢您的回复。 1)我很难找到与&lt;div class="section" id="by-id"&gt; 关联的CSS 样式。我使用了 Chrome 的检查元素工具(将鼠标移到 By ID 部分),我可以找到 font-size 属性。 2) 您能否在不使用 findElement(By.id()) 方法的情况下向我展示显示 font-size 值的代码?谢谢!
  • 我不明白你在问什么。你提到你的代码得到了 13px,这是正确的,现在你说的是I have trouble to find the CSS style associated with the &lt;div class="section" id="by-id"&gt;。我告诉你不用findElements,现在你说你不要findElement?请再仔细看看。
  • @user1177636 很抱歉造成混乱。我使用 Firefox 的 Web 开发者插件(版本 21.0)。我正在尝试查找样式表上显示字体大小值为 13px 的位置或位置。 2) 如果不使用driver.findElements(By.id("by-id")); 和ele.getCssValue("font-size"),您将如何识别与
    关联的字体大小值?
  • 在 HTML 面板中,有一个名为 computed 的侧边栏,font-size 在那里。对于另一个问题,您看到findElements 和findElement 之间的区别了吗?

标签: java css selenium selenium-webdriver


【解决方案1】:

是的,完全正确。

这是通过 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"));
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      公共类 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

      【讨论】:

        【解决方案4】:

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

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

        参考:-

        【讨论】:

          猜你喜欢
          相关资源
          最近更新 更多
          热门标签