Friday 15 March 2013

java - Selenium WebDriver - getCssValue() method -


I am doing a practice to use the cssGetValue method to get value from the CSS properties of a particular web element .

I have 2 questions:

  1. Why cssGetValue returned the value value 13px, which is the web element actually referenced method 1a. I have to get the CSS property for the label "According to ID". How do I modify my code so that I can get the CSS property value for the id = "by-id" section?

  2. The driver.close () method I used, but it can not close the browser after the 'script' expires. Please explain to me that the driver.close () method is the case Why does not work in

    Here is my code piece:

      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 base URL = "http://docs.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example"; @Test Public Zero Address Elements () {driver.get (baseUrl); {List & lt; WebElement & gt; Element = driver.finded elements (By.id ("by-id")); System.out.println ("Number of Elements:" + elements.size ()); (WebElement ele: Elements) for {System.out.println (ele.getTagName ()); System.out.println ("Get text for id = 'by-id'" for web element); Println ("----------------------------------------------- ------------- "); Println (ele.getText ()); Println ("----------------------------------------------- ------------- "); Println (ele.getAttribute ("id")); Println (ele.getCssValue ("font-size")); }} Finally {//driver.close (); Driver.quit (); }}}      

    Yes, all right.

    Here's a screenshot to find font-size via firebug.

    Enter image details here

    Since IDs are unique ( At least for this page), you do not need the findElements id = id = by-id and get a list of elements instead, to get the element directly Use the findElement .

      try {WebElement byId = driver.findElement (By.id ("by-id")); Println (byId.getTagName ()); System.out.println (get text for web element with "id = 'by-id' '); Println (" ---------------------- --------------------- ----------------- "); Println (byId.getText ()); Println ("----------------------------------------------- ------------- "); Println (byId.getAttribute (" id "); Println (byId.getCssValue (" font-size "));}}    

No comments:

Post a Comment