White Box Testing
Test your code
   Home      selenium2WebDriver-LocatingUIWebElement
 
Selenium 2 webdriver finding web UI element
 
Firebug and Firepath are the used to locate UI element from webpage.
 
To locate UI element from webpage through firebug use marked points


 
 
1) Find the web element by name attribute
     For example to find element whose code look like this <input type="button" name="name-012" value="Name-Button">
           
                        someElement = driver.findElement(By.name("name-012"));
 
                                               
2) Find the web element by ID attribute
     For example to find element whose code look like this <input type="button" id="id-012" value="ID-Button">
 
                        someElement = driver.findElement(By.id("id-012"));
 
                                               
3) Find the web element by Class Name
      For example to find element whose code look like this
 
  <div class="shape">
     <span>circle</span>
     </div>
     <div class="shape">
     <span>square</span>
     </div>
 
                        elementList = driver.findElements(By.className("shape"));
                        for(WebElement e:elementList)
                        {
                                    System.out.println("" + e.getText());
                        }
 
                       
4) Find the web element by Tag Name
      For example to find element whose code look like this  <title>White Box Testing</title>
 
                        someElement = driver.findElement(By.tagName("title"));
                       
 
5) Find the web element by Link Text
     For example to find element whose code look like this <a href="http://www.google.com/search?q=whiteboxtest">whiteboxtest</a>
 
                        someElement = driver.findElement(By.linkText("whiteboxtest"));
                       
                       
6) Find the web element by Partial Link Text
     For example to find element whose code look like this <a href="http://www.google.com/search?q=hello">search hello</a>
 
                        someElement = driver.findElement(By.partialLinkText("hello"));
 
                                               
7) Find the web element by CSS
     For example to find element whose code look like this
 
  <div id="food">
    <span class="dairy">milk</span>
     <span class="dairy aged">cheese</span>
     </div>
 
                        someElement = driver.findElement(By.cssSelector("#food span.dairy.aged"));
 
                                               
8) Find the web element by XPath
 
     This is the most powerful mechanism to locate web UI element. Locating a element should be avoided by id or name if the dynamic id generated at runtime
 
    Firebug and Firepath are the tools used to extract Xpath of an element.
    To locate xpath of UI element from webpage through firebug and firepath use marked points
 
 

    For example to find element whose xpath value is .//*[@id='food']/span[1]
 
                        someElement = driver.findElement(By.xpath(".//*[@id='food']/span[1]"));
 
 
 

Download example code HelloWorldLocatingUIElement